June 15, 2021

File Manipulation in Linux

Files and directories (another name for folders) are at the heart of Linux, so being able to create, view, move, and delete them from the command line is very important and quite powerful. These file manipulation commands allow you to perform the same tasks that a graphical file explorer would perform.

Create an empty text file called myFile:

touch myFile

Rename myFile to myFirstFile:

mv myFile myFirstFile

View the contents of a file:

cat myFirstFileView the content of a file with pager (one screenful at a time):

View the content of a file with pager (one screenful at a time):

less myFirstFile

View the first several lines of a file:

head myFirstFile

View the last several lines of a file:

tail myFirstFile

Edit a file:

vi myFirstFile

See what files are in your current working directory:


Create an empty directory called myFirstDirectory:

mkdir myFirstDirectory

Create multi path directory: (creates two directories, src and myFirstDirectory)

mkdir -p src/myFirstDirectory

Move the file into the directory:

mv myFirstFile myFirstDirectory/

You can also rename the file:

[email protected]:~$ mv myFirstFile secondFileName

Change the current working directory to myFirstDirectory:

cd myFirstDirectory

Delete a file:

rm myFirstFile

Move into the parent directory (which is represented as ..):

cd ..

Delete an empty directory:

rmdir myFirstDirectory

Delete a non-empty directory (i.e. contains files and/or other directories):

rm -rf myFirstDirectory

Make note that when deleting directories, that you delete ./ not / that will wipe your whole filesystem.

File/Directory details

The ls command has several options that can be used together to show more information.


The l option shows the file permissions, size, and last modified date. So if the root directory contained a dir called test and a file someFile the command:

[email protected]:~$ ls -l

Would output something like

-rw-r--r-- 1 user users 70 Jul 22 13:36 someFile.txt
 drwxrwxrwx 2 user users 4096 Jul 21 07:18 test

The permissions are in the format of drwxrwxrwx. The first character represents the file type d if it’s a directory – otherwise. The next three rwx are the permissions the user has over the file, the next three are the permissions the group has over the file, and the last three are the permissions everyone else has over the file.

The r of rwx stands for if a file can be read, the w represents if the file can be modified, and the x stands for if the file can be executed. If any permission isn’t granted a – will be in place of r, w, or x.

So from above user can read and modify someFile.txt but the group has only read-only rights.

To change rights you can use the chmod ### fileName command if you have sudo rights. r is represented by a value of 4, w is represented by 2, and x is represented by a 1. So if only you want to be able to modify the contents
to the test directory.

Owner rwx = 4+2+1 = 7
Group r-x = 4+0+1 = 5
Other r-x = 4+0+1 = 5

So the whole command is

chmod 755 test

Now doing a ls -l would show something like

drwxr-xr-x 2 user users 4096 Jul 21 07:20 test

Readable Size

Used in conjunction with the l option the h option shows file sizes that are human readable. Running

[email protected]:~$ ls -lh

Would output:

total 4166
 -rw-r--r-- 1 user users 70 Jul 22 13:36 someFile.txt
 drwxrwxrwx 2 user users 4.0K Jul 21 07:18 test


To view hidden files use the a option. For example

[email protected]:~$ ls -a

Might list


Total Directory Size

To view the size of the current directory use the s option (the h option can also be used to make the size more readable).

[email protected]:~$ ls -s

total 4166
someFile.txt test

Recursive View

Lets say test directory had a file anotherFile and you wanted to see it from the root folder, you could use the R option which would list the recursive tree.

[email protected]:~$ ls -R

 someFile.txt test

Vedant Singh

Linux System Administrator | Entrepreneur | Founder Techmion. I have started my career as a Linux system Administrator after that quit my job to start something new that excites me and started a Tech Company in order to provide the best technology to my clients. My Company website link: https://www.techmion.com/ For Bulk, SMS-Marketing follows the link : https://www.pingsms.in/

View all posts by Vedant Singh →

Leave a Reply

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


Ad Blocker Detected!

We've noticed that you are using an ad blocker. Advertising helps fund our server cost and keep it truly independent. It helps to build our content creator team. So please disable your ad blocker, and help us to keep providing you with free- great content - for free. Thank you for your support.