File Management Commands in Linux

File management is the process of creating, deleting, copying, and moving files or directories for organizing files logically.

Copy a file or directory

cp is used to copy a file or directory from one location to another.

cp file1 file2

Copies file1 with the name file2 in the current directory

cp file1 file2 /tmp/

Copies file1 and file2 with the same name to the /tmp/ directory

cp file1 /tmp/myfile

Copies file1 with a new name, myfile, to the /tmp/ directory

cp -r backup /tmp/

Copies the backup directory recursively to the /tmp/ directory

The examples

Rename or move

mv is used for two purposes. Firstly, it renames a file or directory if the source and destination path of the file are in the same directory. Secondly, it is used to perform cut and paste (move) operations when the source and destination directory are different.

Some of the most frequently used options with the mv command are as follows:

mv file1 file2

Renames file1 with the name file2 in the current directory

mv file1 file2 /tmp/

Moves file1 and file2 with the same name in the /tmp/ directory

mv file1 /tmp/myfile

Moves file1 with a new name, myfile, to the /tmp/ directory

mv backup /tmp/

Moves the directory with the name backup to the /tmp/ directory

The examples

Create a directory

mkdir is used to create a directory. This command is also used with different options on the command line, including the following:

mkdir backup

Creates a sample directory backup in the current directory

mkdir /tmp/backup

Creates a sample directory backup under the /tmp directory

mkdir -p backup/linux/system

Creates directories with full path backup/linux/system (if parent directories are missing at destination, it will create full path)

mkdir linux windows mac

Creates directories with the name linux, windows, and mac in the current directory

The examples

Delete empty directories

rmdir is used to delete empty directories only. If a directory contains subdirectories or files, then we have to use the rm -rf command as shown in the following command:

rmdir <empty directoryname<

The examples

Delete/remove a file

rm is used to delete/remove a file from the filesystem. This command also has multiple options, which are to be used with care as a file once deleted cannot be restored from the trash (the recycle bin of Linux) in command-line mode.

This is a table listing options frequently used with the rm command and their descriptions:


Removes a file

rm -f

Forcefully removes a file

rm -i

Interactively removes a file by prompting before each removal (use this if you are uncertain of the filename)

rm -rf

Forcefully remove a directory recursively (use this option very cautiously)

Create links

The ln command is used to create links. There are two types of links in Linux, hard links and soft links, which is also known as symbolic link/symlink. The soft links of files and directories can be considered equivalent to the Windows shortcut for files and folders respectively:

ln file1 file2

Hard link creation

ln -s file1 file2

Soft link creation

The examples

Add Comment