Linux Commands - Working with files and directories

Reference Linux

Next →

The following commands are used to work with files and directories.

pwd

This command displays the present working directory where you are currently in.

In the following example I am inside yusufshakeel directory which is inside the home directory.

$ pwd
/home/yusufshakeel

ls

This command will list the content of a directory.

In the following example we are listing the content of a directory.

$ ls 
happy  helloworld.txt  super

ls -la

This command will list all the content of a directory including the hidden files and directories.

In the following example we are listing all the content of a directory.

$ ls -la
total 0
drwxr-xr-x  5 yusufshakeel  yusufshakeel  160 Sep  6 02:53 .
drwx------+ 8 yusufshakeel  yusufshakeel  256 Sep  6 02:53 ..
drwxr-xr-x  2 yusufshakeel  yusufshakeel   64 Sep  6 02:53 happy
-rw-r--r--  1 yusufshakeel  yusufshakeel    0 Sep  6 02:53 helloworld.txt
drwxr-xr-x  2 yusufshakeel  yusufshakeel   64 Sep  6 02:53 super

mkdir

This command will create a new directory, provided it doesn't exists.

In the following example we are creating a new directory example.

$ mkdir example

mkdir -p

This command will create nested directories.

In the following example we are creating world directory which is inside the hello directory which is inside the example directory.

$ mkdir -p example/hello/world

rmdir

This command will remove/delete an existing directory, provided it is empty.

In the following example will are removing/deleting an existing directory example.

$ rmdir example

cd

This command is used to change directory.

In the following command we are moving to root directory.

$ cd /

In the following command we are moving to /var/www/html directory.

$ cd /var/www/html

cd ..

This command will take us one level up the directory tree.

$ cd ..

Example: If we are inside world directory which is inside the hello directory i.e. /hello/world then, cd .. will take us one level up to the hello directory.

touch filename

This command will creates a new file.

In the following example we are creating a new file hello.txt.

$ touch hello.txt

rm filename

This command will delete a file.

In the following example we are deleting a file by the name hello.txt.

$ rm hello.txt

rm -f filename

This command forcefully deletes a file.

In the following example we are forcefully deleting a file by the name hello.txt.

$ rm -f hello.txt

rm -r directory

This command deletes a directory recursively along with its content.

In the following example we are deleting a directory example along with its content.

$ rm -r example

rm -rf directory

This command forcefully and recursively deletes a directory along with its content.

In the following example we are forcefully deleting a directory example along with its content.

$ rm -rf example

Be careful while performing delete operation.

cp file1 file2

This command copies the content of file file1 into file file2.

If file file2 doesn't exists then it is created. If it exists then its content is overwritten.

In the following example we are copying the content of file hello.txt to hi.txt.

$ cp hello.txt hi.txt

cp -r dir1 dir2

This command copies the content of directory dir1 into directory dir2.

If directory dir2 doesn't exists then it is created. If it exists then its content is overwritten.

In the following example we are copying the content of directory awesome to directory superawesome.

$ cp -r awesome superawesome

mv - rename files and directories

We can use mv command to rename files and directories.

In the following example we are renaming file hello.txt to hi.txt.

$ mv hello.txt hi.txt

In the following example we are renaming directory awesome to superawesome.

$ mv awesome superawesome

mv - move files and directories

We can also use mv command to move files and directories.

In the following example we are moving file hello.txt from directory example to directory awesome.

$ mv /example/hello.txt /awesome/

In the following example we are moving directory example inside /var/www/html directory.

$ mv example/ /var/www/html/

cat filename

This will print the content of a file.

In the following example we will get the content of the file hello.txt in the terminal.

$ cat hello.txt 
The quick brown fox jumps over the lazy dog.

head filename

This command will print the first 10 lines of a file.

In the following example we will get the first 10 lines of the file fruits.txt.

$ head fruits.txt 
Apple
Banana
Cucumber
Dates
Eggfruit
Fig
Grapes
Hackberry
Imbe
Jackfruit

tail filename

This command will print the last 10 lines of a file.

In the following example we will get the last 10 lines of the file fruits.txt.

$ tail fruits.txt
Quince
Raspberries
Strawberries
Tangerine
Ugni
Voavanga
Watermelon
Xigua
Yangmei
Zuchinni

tail -f filename

This will print the last 10 lines of a file and will keep printing new lines as they get appended to the file.

This is useful when checking live activity logs.

In the following example we will get the last 10 lines of the file log.txt and the new content as they get appended to the file.

$ tail -f log.txt
Next →

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT