A file is a named collection of related information that is recorded on secondary storage such as an optical disk. A directory is a location for storing files on your computer. Directories are usually found in hierarchical file systems. Linux organizes its file system in a hierarchical way. The directory structure in Linux must be created in such a way that it is easy to access whatever file is needed.
File manipulation - creating a file, deleting a file, copying a file - is very common in Linux. There are many commands in Linux that enable the user to manipulate files. Here, our main focus is the Linux commands which manipulate a file but not it's contents.
In Linux, there are certain special characters called Wildcards which enable the users to specify a particular group of files based on certain features.
Here are a few wildcards which are very useful for file manipulation:
A few examples of wildcard usage are:
A few file and directory manipulation commands in Linux are:
1. cp - The cp command is used to data from one file to another file.
Syntax:
This command copies the contents of file1 to file2.
If file2 does not exist in the directory, it is created and then the contents are copied. If file2 exists, then the contents of file2 are overwritten by the contents of file1.
A few options of cp command are:
i) cp -i - The -i command is used to run the cp command in interactive mode. So, if there already exists a file named file2, then in interactive mode, the command will ask the permission of the user before overwriting the existing file.
ii) cp -r - By default, the cp command works for files. To copy one directory into another, the -r option of the cp command is used.
Syntax:
Here, the 'dir' directory will be copied to the Desktop directory (or simply Desktop).
iii) cp -u - This command copies the file only when the file being copied is newer than the file it is being copied to.
For example, if the user wants to copy a file named file1 to the directory Desktop but the Desktop directory already has a file named file1, then file1 will be copied to Desktop only if file1 in Desktop is older than the file being copied.
In simple words, this command is used to update a file.
Syntax:
2. mv - The mv command (also called move command) renames or moves files and directories based on its usage.
The syntax to replace a file is:
If file2 exists, then its content is overwritten by that of file1. If file2 does not exist, then file1 is renamed as file2.
Similarly, to replace a directory, the syntax is:
If dir2 exists, then dir1 is moved into dir2 but if dir2 does not exist, then dir1 is renamed dir2.
To move files to a new directory, the following syntax is used:
If dir1 does not exist, then mv command exits with an error.
A few options of mv command are:
i) mv -i - Like cp command, the mv command also has an interactive mode, If the mv command is to overwrite an existing file while operating in interactive mode, then it has to ask the permission of the user
before doing so.
ii) mv -n - The -n option of the mv command stands for "Never Overwrite". When being used in this mode, the mv command never overwrites an existing file.
iii) mv --version - This command is used to display the version of the mv command being used.
iv) mv -f - The -f(or force) option of mv overwrites the write protection of a file. If the file being overwritten is write-protected, then it asks for confirmation before doing see. The -f option overwrites this write protection.
3. rm - The rm command is called the "remove" command. It is used to delete files and directories from the Linux system.
The syntax for rm is:
This command deletes both file1 and file2 from the working directory.
A few options of the rm command are:
i) rm -i - The -i option runs the rm command in interactive mode. This means that the user is prompted every time before a file is being deleted by the rm command.
ii) rm -d - The -d option enables the user to delete multiple empty directories in the system through a single command.
iii)rm -r - The -r option of the rm command allows the user to delete non-empty directories and all the files they contain recursively.
iv) rm -v - The -v option (also called verbose) gives more information to the user about the file (or directory) being deleted.
v) mkdir - The mkdir command is the "make directory" command which lets the user create new directories. This command can create multiple directories at once.
The syntax of mkdir is:
A few options of the rm command are:
i) mkdir --version - This command shows the user the version of mkdir being used by the system.
ii) mkdir -v - The -v (or verbose) command display information related to the directories created.
iii) mkdir - m - The -m option allows the user to set permissions for the files in the directory.
5. cat - The cat command is also called the "concatenate" command. It is used to read files and display their content in the output. It can also be used to create new files and concatenate existing files.
The syntax of cat command is:
A few option of the cat command are -
i) cat -n - The -n option of cat is used to display the contents of a file along with their line numbers.
ii) > - The '>' option (or symbol) is used with the cat command to copy the contents of one file to another.
Here, the contents of file are copied to file2 and if file2 exists, its contents are overwritten.
iii) >> - The '>>' option (or symbol) is used to append the contents of one file to another file.
The '>' can be used along with the cat command to create new files as well.
The above command will create a new file in the directory named file1.txt.
Name | Views | Likes |
---|---|---|
Usermod command in Linux | 4510 | 1 |
SSH Command In Linux | 788 | 7 |
Introduction to Named Pipes | 1057 | 3 |
Linux KILL Command | 2782 | 5 |
Soft Link and Hard Link In Linux | 421 | 2 |
Vi Editor in Linux | 594 | 6 |
FDisk Command in Linux | 540 | 5 |
How to view the History in Linux | 448 | 2 |
Process Manipulation in Linux | 1614 | 4 |
Export command in Linux | 410 | 1 |
ID command in Linux | 413 | 1 |
Netstat Command in Linux | 623 | 3 |
Chmod Command in Linux | 437 | 1 |
Compressing Files in Linux | 354 | 1 |
Crontab in Linux | 477 | 1 |
File Manipulation in Linux | 615 | 5 |
Curl Command in Linux | 505 | 1 |
Env Command in Linux | 397 | 5 |
LSOF Command in Linux | 406 | 1 |
User Manual in Linux | 432 | 1 |
Whereis command in Linux | 436 | 1 |
4 Lesser Known Commands in Linux | 468 | 6 |
Shell Scripting in Linux | 631 | 4 |
Alias command in Linux | 373 | 1 |
How to Kill a Process using Command Line | 471 | 3 |
Comments