File Manipulation Commands 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 myFirstFile


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:

ls


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. The ls command has several options that can be used together to show more information. Details/Rights 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 format of drwxrwxrwx. The first character represents the file type d if it’s a directory - otherwise. The next three characters, 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


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  Hidden To view hidden files use the a option. For example [email protected]:~$ ls -a


Might list

.profile
someFile.txt
test


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  Outputs 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


Outputs

.:
someFile.txt    test

./test:
anotherFile