File/folder manipulation commands in unix/linux what I know
If we want to work in linux servers, The knowledge of file/folder manipulation is a must. In this article, I'm going to share my knowledge about the linux commands about file/folder manipulation. In linux/unix not only the files and directories are treated as files but also everything. For example, devices, processes are also files.
What refers file/folder manipulation
File/folder manipulation refers the following
Create/edit/delete file or folders
Copy/move and rename files
Some other file operations not manipulation things
Listing files or directories with permissions
View files
View/change directories
Let's list down the main commands in file/folder manipulation area
mkdir
touch
cp
mv
rm
rmdir
Let's talk about them with some short description and examples
mkdir
The full form of mkdir
is making directories where 'mk' stands for making and 'dir' for directories. So, this command is used for creating directories if not exists. If exists, it will not create one.
mkdir src
This command will create the src folder in the current directory.
Now we want to create a hierarchy of directories like this
mkdir src/com/leeonscoding/www
This will not work. Because the parents aren't existed. In here we need one option -p
. So, the parents will also be created.
mkdir -p src/com/leeonscoding/www
If you need the details how those folders created then use another option -v
mkdir -p -v src/com/leeonscoding/www
The output may looks like this
src
src/com
src/com/leeonscoding
src/com/leeonscoding/www
rmdir
Let's remove the folders. In rmdir, rm stands for remove and dir for directory. If we run this command rmdir src
we will get an error. Because the folder isn't empty, there are sub folders in it. But we can remove an empty folder using rmdir
.
rmdir src/com/leeonscoding/www
The folder 'www' will delete. Now what if we need to delete the src folder with sub folders at once. We need the option parent -p
rmdir -p -v src/com/leeonscoding
Here -v for details(hierarchy) of the removal. The output is
src/com/leeonscoding
src/com
src
But what if there are files inside folder or folders. The rmdir
will not work then. As we need to remove files too then it is a part of file too. I'll talk about it later in this article.
touch
The simple way of creating files is the touch
command. Although the purpose of touch is changing timestamp of a file. In other word, we can also update a file using this command.
mkdir src && cd src && touch file1.txt file2.txt file3.txt
Here I've created 3 files. Let's check the timestamps.
ls -l
A sample output
total 0
-rw-r--r-- 1 sufian staff 0 Aug 19 13:30 file1.txt
-rw-r--r-- 1 sufian staff 0 Aug 19 13:30 file2.txt
-rw-r--r-- 1 sufian staff 0 Aug 19 13:30 file3.txt
Now update the timestamp of file1.txt. For being sure, I've put some text in that file. Now, again execute the touch
command.
touch file1.txt
Again run the ls -l
command
A sample output
total 8
-rw-r--r-- 1 sufian staff 12 Aug 19 14:30 file1.txt
-rw-r--r-- 1 sufian staff 0 Aug 19 13:30 file2.txt
-rw-r--r-- 1 sufian staff 0 Aug 19 13:30 file3.txt
Notice that the timestamp for file1.txt is changed from 13:30
to 14:30
. Also you may notice the content of the file is unchanged. So, the file timestamp is only updated.
rm
The rm
command is used for removing files, directories that contains files, device nodes etc.
rm file2.txt file3.txt
2 files will be deleted. Now let's try to remove the src directory. Using rm src
will not work. We have to add an option -r
here which represents recursive.
rm -r src
Now the src with its all child files and folders will be removed.
There are so many options in rm but some are useful I think. One is -i
. Using this option, before deleting a file it will ask for your confirmation. If you press y
, this command will delete the file and if you press n
this command will keep the file.
rm -i file1.txt
Another one can be -d
. Instead rmdir we can remove an empty directory using this option.
mv
The mv
stands for move. In linux this command is used for 2 purposes
Moving files/folders from one directory to another.
Rename files/folders.
Let's move some files first.
mv src/java/lang/*.java src/java/util
In this code example, every file which has the extension .java
in the directory src/java/lang
move to the folder src/java/util
directory.
Now, Create the same file(with same content) again in src/java/lang
and repeat the same thing. It is time for overwrite. If we need a prompt for overwriting like this
overwrite src/java/util/String.java? (y/n [n])
Then we need an option -i
mv -i src/java/lang/*.java src/java/util
If we don't overwrite at all then use option -n
and if we need overwrite without any question then -f
If you want to know which files are affected like this output
src/java/lang/String.java -> src/java/util/String.java
Then use -v
Now let's rename a file
mv -v src/java/util/Object.java src/java/util/Object2.java
Here we're not moving any file. We keep the file where it is. But change the name. You can check it using this command
ls src/java/util
cp
The cp
stands for copy. It is similar to mv
but copy files or directories.
cp src/java/lang/*.java src/java/util
It has similar options like
-i for interactive mode for overwrite files.
-n for not overwriting.
Unlike mv in cp copying a directory which has nested folders and multiple files, need an option -r (recursive) like we use in rm.
cp -r src project1
In this code example, I've copied one directory to another directory.
When we copy one file to another, the ownership and timestamp also changed. To preserve the originals we should use the option -p
. Also, don't forget to use -v
for checking what are the changes.
Replace text content from one file to another using cat
cat test1.txt > test2.txt
Here cat command will print everything from the test1.txt and using the redirect operator(>) the output will redirect to test2.txt. Nothing in test2.txt will exist then.
Conclusion
Hope this article is understandable and you guys learn something from this if you are new. If you like this article don't forget to give me a thumb. Happy coding.
Subscribe to my newsletter
Read articles from Asfaq Leeon directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by