Intro bash commands

SSHing into a server and running commands can be rather daunting. I remember the first couple times I was running commands on a workstation or server, I didn’t quite understand what or why I was doing or what really was happening. Hopefully, this information will help you with your journey into Linux.

pwd

There really isn’t much to this command, it just prints the name of the current/working directory.

root@host:~# pwd
/root

cd

There really isn’t much to this command either, you just provide the directory you want to go.

root@host:~# cd /home/
root@host:/home# pwd
/home

Or if you don’t give the command an option, it takes you to the home directory of the user you are logged in as.

root@host:/home# cd
root@host:~# pwd
/root

ls

This command prints the contents of the current working directory. Without any flags, it prints only files and directories. If you use the -l flag, it provides a lot more information such as permissions, ownerships, file size, and last modified time. The -h flag turns the file size into human readable format.

root@host:~# ls
meme.header  myperl.pl  mysite.sql
root@host:~# ls -l
total 880
-rw-r--r-- 1 root root   3979 Dec 15 04:44 meme.header
-rw-r--r-- 1 root root    579 Dec 26 04:08 myperl.pl
-rw-r--r-- 1 root root 890065 Jan 31 08:27 mysite.sql
root@host:~# ls -lh
total 880K
-rw-r--r-- 1 root root 3.9K Dec 15 04:44 meme.header
-rw-r--r-- 1 root root  579 Dec 26 04:08 myperl.pl
-rw-r--r-- 1 root root 870K Jan 31 08:27 mysite.sql
root@host:~#

df

This command shows the current disk usage of each partition/file-system. The -h flag makes the output human-readable.

root@host:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            991M     0  991M   0% /dev
tmpfs           201M   11M  190M   6% /run
/dev/vda1        20G  5.5G   14G  29% /
tmpfs          1001M     0 1001M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs          1001M     0 1001M   0% /sys/fs/cgroup
tmpfs           201M     0  201M   0% /run/user/0
root@host:~#

cp

Warning! This can be a destructive command, potentially overwriting an existing file!

This command copies a file, useful when you need to take a backup before modifying a file. Applying the -v flag provides verbose output. Another useful flag is -i which prompts you if you want to overwrite the existing file. The first argument the command takes is the file you want to copy and the second argument is what you want to call the copy. For example, if I want to create a copy of php.ini named php.ini.bak, I would run the following:

root@host:~# cp -iv php.ini php.ini.bak
cp: overwrite 'php.ini.bak'? y
'php.ini' -> 'php.ini.bak'

mv

Warning! This can be a destructive command, potentially overwriting an existing file!

This command is similar to cp except it doesn’t leave the original file. It is used to move or rename files. Let’s say I want to move the file out of the way rather than make a copy of it, mv is the command to use. Just like cp takes two arguments, the same goes for mv and VERY similar flags are used as well. In this example, you can see that the file was renamed from php.ini to php.ini.broken.

root@host:~# mv -vi php.ini php.ini.broken
'php.ini' -> 'php.ini.broken'
root@host:~# ls -1
meme.header
myperl.pl
mysite.sql
php.ini.broken
tmp
root@host:~#

rm

Warning! This is a destructive command!

This command has the potential to completely trash your system. One wrong keystroke and you better hope you have backups. For example, if you mean to remove /home/user/tmpdir but you instead /home/ user/tmpdir, your home directory is now gone.

The -v (verbose) flag is extremely useful so you can what all is happening with your command. By default, you cannot use rm to remove a directory, even with the -f (force) flag:

root@host:~# rm -v dir1/
rm: cannot remove 'dir1/': Is a directory

root@host:~# rm -fv dir1/
rm: cannot remove 'dir1/': Is a directory

It can be removed by using the -r (recursive) flag:

root@host:~# rm -rv dir1/
removed directory 'dir1/'

If you have multiple file and directories within a directory you are trying to remove, it will recurse as far as possible to remove everything. As mentioned above, this can be quite dangerous if you mistype a path.

root@host:~# rm -rv dir1/
removed 'dir1/file2'
removed directory 'dir1/subdir'
removed 'dir1/file1'
removed 'dir1/file3'
removed directory 'dir1/'

rmdir

Warning! This is a destructive command!

This is the preferred command to remove directories. Just provide the directory to be removed and it will (attempt to) remove it. In this first example, you can see that it fails.

root@host:~# rmdir dir2
rmdir: failed to remove 'dir2': Directory not empty

Best practice is that you check the directory to see what’s in it instead of just blindly attempting to remove things. In this case, I have checked and it’s just a bunch of empty directories. If I use the -p (parent directories) flag and provide the most bottom directory, it will remove that bottom directory and everything on up. Similar to the recursion flag of rm in the above examples.

root@host:~# rmdir -vp dir2/subdir/yad
rmdir: removing directory, 'dir2/subdir/yad'
rmdir: removing directory, 'dir2/subdir'
rmdir: removing directory, 'dir2'

What if there are files in any of the directories? Great question! I created some files in dir2/subdir (file1 – file10).

root@host:~# ls -R dir2
dir2:
subdir

dir2/subdir:
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9  yad

dir2/subdir/yad:

Because rmdir is used for removing directories, it can’t remove those files which prevents the directory from being removed because it’s not empty.

root@host:~# rmdir -vp dir2/subdir/yad
rmdir: removing directory, 'dir2/subdir/yad'
rmdir: removing directory, 'dir2/subdir'
rmdir: failed to remove directory 'dir2/subdir': Directory not empty

less

Viewing files couldn’t be easier with less. Just tell it what file you want to look at and it opens it up.

root@host:~# less -N alphabet

And all it takes is pressing q to close out!

Leave a Comment

Your email address will not be published. Required fields are marked *