Using the Linux command line for basic file handling

Linux offers several advantages over the more popular Windows and macOS operating systems. Linux operating systems are free, reliable, secure, and provide greater control over the entire operating system in general. Most Linux operating systems are now GUI powered and support almost all the regular graphical actions such as arranging file systems, including directories, deleting, updating, or renaming files or folders, and more. However, the Linux operating system allows you to perform all of these actions in a much quicker and efficient way using the command line. Using the command line offers several advantages, such as repetitive scripting tasks, performing forceful actions, and attributing a specific task with several options. While the idea of using the command line to perform everyday operations can be daunting initially, they are far more powerful, flexible, and efficient than using the GUI.

linux file handling

Here are the essential Linux commands everyone should know to control your files and folders.

Moving and renaming files with the Linux command line

Moving a file is an operation that simply moves a file from one file directory location to another. So, for us to use commands to move a certain file or files from one location to another, we will need the location paths. The command we use in Linux to move files is mv, which is just a short form for the move. The same command is also used to rename a file in Linux.

Here is the basic format for the move command in Linux:

mv [-option] /path/to/source path/to/destination

In the above command, [option] is optional and is meant to include special options to be performed along with moving files/folders. The command will move the file or folder specified in the source path to the destination path. If the destination already contains the file or folder being moved, then the content in the destination will be replaced by the content in the source.

You can also use the mv command to rename files. Here is an example:

mv file1 file2

Using the above command, we are renaming file1 to file2 within the same directory location.

Copying files

Robocopy
Shutterstock

cp is the Linux command for copying files or folders from one directory path to another. The essential difference between a move operation and a copy operation is that a move operation deletes the file from the source after moving, whereas, with a copy operation, the file remains in both source and destination after copying.

Here is the syntax for copy in Linux:

Cp [-option] /path/to/source path/to/destination

In the above command, [option] is optional, while the source specifies the path for the file or folder which needs to be copied. The destination specifies the location where the file/folder copied from the source will be pasted.

Copying files using rsync

rsync (remote sync) is a powerful tool in Linux, which is commonly used to copy and synchronize the files and directories remotely as well as locally. This command is usually used to perform data backups and to achieve mirroring between two Linux machines. This command consumes less bandwidth and is very efficient. Moreover, it supports copying links, devices, owners, groups, as well as permissions.

The basic syntax of rsync command is:

rsync [options] /path/to/source path/to/destination

If you do not have rsync package installed in your Linux machines you can download the package using the following commands:

yum install rsync (for Red Hat-based Linux systems)
apt-get install rsync (for Debian-based Linux systems)

The options for mv and cp command include:

  • -i for interaction: Asks the user for confirmation before performing a move/rename or copy operation.
  • -f for force: Overrides all the interactivity and executes the mv command without any prompts.
  • -r for recursive: Meant to copy all the subdirectories and files in the source path while preserving the file structure and hierarchy.
  • -v for verbose: Meant to show the files being moved/copied one by one. This can be used to move directories instead of single files.

Deleting files

File deletion is done using rm command in the Linux command line, which is just the short form of remove. The delete command also supports a few options, but they are optional. If the file you want to delete is in the present working directory, then the basic command for deleting a file is as follows:

rm [option] filename

For example:

rm sample.txt

You can also delete more than one file by simply specifying the file names separated by space. For example:

rm sample1.txt sample2.txt sample3.png

The above command will remove all the files mentioned above from the present working directory.

If the file you wish to delete is not in the present working directory, then you can delete the file by specifying the path to the file. For example:

rm /path/to/the/file/fileToBeDeleted.txt

The remove method comes with a powerful wildcard search option that can be used to delete multiple files easily. The * represents multiple files, while the ? represents a single file.

rm *.jpg

The above command will delete all the .jpg files from the present working directory.

rm *.???

The above command will delete all the files irrespective of the name with an extension of three characters. Meaning, all the files with an extension such as .png .jpg .mp4 .mp3 will be deleted. However, files with other extension character lengths such as .py (python files), .jpeg will not be deleted.

The delete method also supports various options such as:

  • -d to remove an empty directory.
  • -r to recursively remove all the files and subdirectories inside the file/folder specified to be deleted.
  • -f to forcefully delete something without asking for prompts.

Also, keep in mind that, unlike traditional delete on Windows or macOS, a file deleted using the rm command will not be sent to the recycle bin or trash but will be removed from the file system.

While all these commands might seem to be complicated initially, they can be very useful to perform some powerful file handling operations — and get the job done quickly.

Featured image: Shutterstock

Leave a Comment

Your email address will not be published.

Scroll to Top