How to pass RHCE/RHCT exam! – part 3

Today we’re going to focus on how to use standard command line tools (e.g. ls, cp, mv, rm, tail, cat, etc.) to create, remove, view, and investigate files and directories.

If you are a beginner, the first thing I suggest is to check the current directory to avoid messing up things. This is simply done with pwd (print working directory):

[root@server ~]# pwd
/root
[root@server ~]#

Another basic command is ls. This just lists the files and directories inside the path given as an argument. If no path is specified, it will list all the files and directories from the current location:

[root@server ~/exam]# ls
directory1  file1
[root@server ~/exam]#
[root@server ~/exam]# ls /etc/httpd/
conf  conf.d  logs  modules  run
[root@server ~/exam]#

There are also hidden files and directories in GNU/Linux. These start with a dot “.” and can be seen by using the -a option for ls. To list the contents of a directory as a column, you may use the -l parameter. And to have the file sizes in a human readable form, just use -h.

[root@server ~/exam]# ls -a
.  ..  directory1  file1  .hidden
[root@server ~/exam]# ls -l
total 4
drwxr-xr-x 2 root root 4096 May  1 23:07 directory1
-rw-r--r-- 1 root root    0 May  1 23:06 file1
[root@server ~/exam]# ls -alh
total 20K
drwxr-xr-x  4 root root 4.0K May  1 23:12 .
drwxr-x--- 16 root root 4.0K May  1 23:06 ..
drwxr-xr-x  2 root root 4.0K May  1 23:07 directory1
-rw-r--r--  1 root root    0 May  1 23:06 file1
drwxr-xr-x  2 root root 4.0K May  1 23:12 .hidden
[root@server ~/exam]#

The basic tool to copy files or directories is named cp. To copy a whole directory you must use the -r option for recursive:

[root@server ~/exam]# ls
directory1  file1
[root@server ~/exam]#
[root@server ~/exam]# cp file1 file2
[root@server ~/exam]#
[root@server ~/exam]# ls
directory1  file1  file2
[root@server ~/exam]#

To copy a directory:

[root@server ~/exam]# ls
directory1  file1  file2
[root@server ~/exam]# cp -r directory1/ directory2
[root@server ~/exam]# ls
directory1  directory2  file1  file2
[root@server ~/exam]#

For more options, you may check the manual page for cp using

man cp

There is also a tool available for renaming/moving files or directories. It’s called mv and may be used like this:
-to rename a file

[root@server ~/exam]# ls
directory1  directory2  file1  file2
[root@server ~/exam]# mv file1 file1-renamed
[root@server ~/exam]# ls
directory1  directory2  file1-renamed  file2
[root@server ~/exam]#

-to rename a directory

[root@server ~/exam]# ls
directory1  directory2  file1-renamed  file2
[root@server ~/exam]# mv directory2/ directory2-renamed
[root@server ~/exam]# ls
directory1  directory2-renamed  file1-renamed  file2
[root@server ~/exam]#

-to move a file/directory to another location just specify the full/relative path

[root@server ~/exam]# mv directory2-renamed/ directory1/directory2/
[root@server ~/exam]# ls
directory1  file1-renamed  file2
[root@server ~/exam]# ls directory1/
directory2
[root@server ~/exam]#

Another basic but important tool is rm which is used to remove files or directories. Like in the case of cp, in order to remove a directory the -r (recursive) option must be specified:


[root@server ~/exam]# ls
directory1  file1-renamed  file2
[root@server ~/exam]# rm file2
rm: remove regular empty file `file2'? y
[root@server ~/exam]# rm -r directory1/directory2/
rm: remove directory `directory1/directory2/'? y
[root@server ~/exam]# ls
directory1  file1-renamed
[root@server ~/exam]# ls directory1/
[root@server ~/exam]#

To avoid being asked for confirmation use the -f (force) option, or use the full path for rm command, because rm is actually an alias to /bin/rm -i (interactive):

[root@server ~/exam]# which rm
alias rm='rm -i'
        /bin/rm
[root@server ~/exam]#

cat is the tool that will display the contents of a file for you. For example to see the contents of a file named example.txt you can use:

[root@server ~/exam]# cat example.txt

1
2
a - This is a letter
This is a test line
3 - And this is a number

[root@server ~/exam]# 

If you want to search for a certain string, you send the output from a given command to a tool named grep:

[root@server ~/exam]# cat example.txt |grep test

This is a test line
[root@server ~/exam]#

This will print out all the lines containing the string test. In our case there was just 1 line.