In this article we are going to learn how to create, move and delete files in a Linux using command-line tools. We will be using the following commands: mv, ls, cp, pwd, find, touch, rmdir, rm, echo, cat and mkdir
The command-line interface is the most effective way to manage Linux file systems.
A directory is a special kind of file used to organize files. A good way to think off files is like the file folders used to organize papers in a file cabinet.
The mkdir commands are used to create directories.
To return to the home directory, use the cd command:
The find command is the handiest tool in Linux. It searches for files and directories in a directory hierarchy based on a user-given expression and can perform user-specified action on each matched file. It can be combined with other tools such as grep.
Find command syntax
$ find [options] [path...] [expression]
- The options attributes controls the treatment of symbolic link, debugging options and optimization method.
- The path… attribute defines the starting directory or directories where find search the files.
- The expression attribute is made up of options, search patterns and actions separated by operators.
When invoking the find command you need to have read permissions on that directory.
An empty file can be created with the touch command. For example,
# touch <filename>
# touch file1
The cat command can be used to view the contents inside a file. Let’s take the example we created above.
# cat file1
It returns nothing because touch creates empty files.
The echo command is used with > to create simple text files. For example, we can add content to our file1 created above with the following command:
# echo welcome > file1
> character instructs the shell to write the output of a command to the specified file instead of your terminal.
Files are moved and renamed with the mv command. For example, let us add some files to our example above.
$ touch file3 file44 $ echo file3 > file3 $ echo file44 > file44
What if we made a mistake with naming files, instead of file4 we typed file44. We fix this with the mv command as follows:
$ mv file44 file4
Files are moved from one directory to another with the mv command
$ mkdir dir1 dir2 dir3
Deleting files and Directories
We use the rm command to delete files and directories.
The rmdir command deletes directories only.
rm will not delete a directory by default, we need to add -r for it to delete the directory.
$ rm -r dir1
Copying files and Directories
The cp command is used to copy files and directories.
# cp [additional_option] source_file target_file
cp file.txt file1.txt
This Linux command creates a copy of the file.txt file and renames the new file to file1.txt.
By default, the
cp command runs in the same directory you are working in. However, the same file cannot exist twice in the same directory. You’ll need to change the name of the target file to copy in the same location.
From the above, we have learned how to create, move and delete files on a Linux system. Try to do a lot of practice to get full grasp of the content.
Check out these related articles