Modern technology gives us many things.

7 Must-Know Linux File Manipulation Commands


Modern Linux desktops have come a long way in regards to letting you manipulate files on your system. However, because of all the overhead resources used by a GUI (such as GNOME or KDE Plasma), when you need to work with more than one file or directory, the command line is often the fastest and most efficient way to get things done.

Learning how and when to take advantage of the Linux command line with just a handful of essential file-manipulation commands will help to enhance your productivity and transform your Linux experience.

Linux Command Line Basics

Before we get into actual commands, here are a few tips to help avoid confusion and errors:

  • The Linux command line is case-sensitive. Capital and lowercase letters are different in Linux. Typing ls will list files in your current directory. Typing Ls or LS will return a command not found error. ls -a and ls -A are also two different commands. Be sure to pay attention to capital and lowercase letters when copying or entering any command.
  • The tilde (~) is shorthand for your home directory. Every user on Linux has a home directory. This directory stores all your personal files. The full path to that directory is usually something like /home/user/ or /var/home/user/. You can substitute ~ anywhere in a command to represent that path. For example, the commands cd /home/user and cd ~ are the same.
  • Typing any command followed by “–help” will often show quick usage examples. Almost every command has switches or arguments that you can use to modify its behavior. Using the command line argument –help will usually explain the most commonly used options. For example, ls –help.
  • Be careful while running any command that starts with the word “sudo.” You can instantly render your entire system inoperable by entering a bad sudo command.


The Linux man Command

Linux ls man page

The single most useful command available on every Linux system is the man command. Man is short for manual.

When you type man followed by any other command, the system will show you a help document that explains—often in great detail—how that command works. Above is the first page of output for the ls manual page (often referred to as the man page).

Explanations and examples of commands described below are meant to show some of the most common use cases. They are far from exhaustive. We encourage you to use the man command to find out more about what you can do with these commands on your own.

The ls Command

The ls command will list the contents of your current directory. There are several common switches used to change the type of output the command returns. Entering ls alone will show a list of the current directory contents.

On most Linux distributions, the list will be sorted alphabetically, divided into columns, and color-coded to help differentiate between files, directories, executables, and other attributes.

Linux ls command output

There are several useful flags that you can use to change the command output. You can either use them separately or combine them into a single argument.

The three commonly used switches are -l, -a, and -h.

# Arrange output in a single-column list
ls -l

# Show all files (include hidden files and directories)
ls -a

# Show byte counts in human-readable form (KB, MB, etc.)
ls -h

# Show all files in a single column in human-readable form
ls -lha

The Linux cd Command

The cd command will change your current directory. You can simply type cd followed by the name of any directory to switch to it or enter a complete path to jump to a specific place.

# Switch to a folder named mystuff in the current directory
cd mystuff

# Move up (or back) one directory from where you are (note the space and two dots)
cd ..

# Change to your home directory
cd ~

# Change to an entirely new path (note the “/” at the beginning)
cd /var/tmp/flatpak-cache/

The cp Command in Linux

To copy one or more files using the command line, use the cp command. You can use cp to copy files from one directory (or drive) to another, or to create a second file with a new name.

The command follows the format cp /original/file.ext /destination/file.ext.

# Make a second copy of a file in the same directory
cp myspecialfile.two

# Copy a file to a folder named newlocation in the home directory
cp myfile ~/newlocation/

# Copy a directory including all files and subdirectories to your home directory
# Note the -r (recursive) switch
cp -r importantdata ~
cp -r importantdata /home/user/

# Copy recursively from one path to another
cp -r /var/tmp/ /home/user/backuptemp

Related: What Is the Linux Command Line and How Do You Use It?

The Linux mv Command

The mv command will move files or directories. It follows the same basic syntax as the cp command. The only real difference is that the move command will erase the source file(s) as the data is moved to its new location.

This command also serves to rename files in Linux. Since Linux has no actual rename command, the job is done by moving a file from one name to another.

# Rename to file.two erasing in the process
mv file.two

# Move all files in the current directory to a new destination
# Note that * matches all files
mv * /new/storage/location/

# Move the contents of one directory into another
mv /home/user/Videos /home/newuser/oldvideofiles

The rm and rmdir Commands

Short for remove, the rm and rmdir commands will remove (as in permanently delete) files and directories. The rm command simply requires the name or path to a file or files to delete. You can delete multiple files by using the * wildcard character.

# Remove a single file in the current directory
rm uselessfile.ext

# Remove all files starting with the letters ‘ab’ in the current directory
rm ab*

# Remove all files starting with ab in a specific directory
rm ~/myfiles/zfiles/ab*

You can use rmdir to remove an empty directory. A common problem when using this command is trying to delete a directory that seems empty but contains hidden files. Use the ls -a command to show hidden files that need to be removed if you get an error.

# Remove an empty directory from the current path
rmdir olddirectory

# Remove a temporary directory from your home directory
rmdir ~/temporary

Removing Directory Trees With rm

The rm command also functions as a powerful utility for removing entire directories and even directory trees. Since the rm command permanently deletes files and directories, you must be extremely careful when issuing a command that traverses directories and acts based on wildcards.

Related: Ways to Get Command- Line Help on Linux

There are two important flags that you need to be aware of. The first is -f. The f stands for force. It turns off the “Are you sure?” prompt that normally comes up when you try to delete a file.

When you run the rm command with the -f switch, it will delete whatever you tell it to delete without checking or asking if you are sure. When you hit Enter, the command is done and there is no going back.

The second switch is -r, which stands for recursive. This will allow the rm command to go down through the directory tree from where the command is issued and act on any files or subdirectories that it finds.

# Delete all files starting with XX in the current directory without confirmation
rm -f XX*

# Delete all files starting with XX in the current directory and all subdirectories
rm -rf XX*

# Delete all files in the current directory without confirmation
rm -f *

# Delete everything in this directory removing any and all files and subdirectories found
# This command should always be used with extreme caution.
rm -rf *

The command rm -rf * is useful when used correctly to erase large amounts of files that are no longer needed. It can remove huge amounts of data in just a second or two. If, however, you are not paying attention to where you are in the directory structure it can be very dangerous.

Running rm -rf * in your home directory, for example, will instantly wipe out all of your personal files. Worse, running the command as the root user, or using sudo, can wipe out your entire operating system.

If you need to use this command, stop, check where you are in your directory tree, think, then stop, look again, and think some more before you hit Enter. There is no going back.

Manipulating Files and Folders Using the Linux Terminal

Armed with these seven Linux commands, you can manipulate your entire file system however you see fit. Learn to use them well, and you will never have to hunt through the menus on your file manager to get something done again.

Best of all, without all the extra processing caused by those desktop file managers, your file maintenance operations will be instantaneous. But if you still need a graphical file manager, Linux has got plenty of them.

10 Best File Managers for Linux Power Users

Having a reliable file manager makes file organization easier. Here’s a list of the best file managers for Linux.

Read Next

About The Author

Source link

Leave A Reply

Your email address will not be published.