Wednesday, April 24, 2013

How to Manage Files from the Linux Terminal: 11 Commands You Need to Know


To use the Linux terminal like a pro, you’ll need to know the basics of managing files and navigating directories. True to the Unix philosophy, each command does one thing and does it well.
Midnight Commander, a full-featured file manager for the Linux terminal, acts as a powerful front end to all these commands.

ls – List Files

The ls command lists the files in a directory. By default, ls lists files in the current directory.

You can also list files recursively — that is, list all files in directories inside the current directory — with ls -R.

ls can also list files in another directory if you specify the directory. For example, ls /home will list all files in the /home directory.

cd – Change Directory

The cd command changes to another directory. For example, cd Desktop will take you to your Desktop directory if you’re starting from your home directory.

You can also specify a full path to a directory, such as cd /usr/share to go to the /usr/share directory on the file system.
cd .. will take you up a directory.

rm – Remove Files

The rm command removes files. Be careful with this command — rm doesn’t ask you for confirmation.

For example, rm file would delete the file named “file” in the current directory. Like with other commands, you could also specify a full path to a file: rm /path/to/file would delete the file at /path/to/file on your file system.

rmdir – Remove Directories

The rmdir command removes an empty directory. rmdir directory would delete the directory named “directory” in the current directory.
If the directory isn’t empty, you can use a recursive rm command to remove the directory and all files in it. rm -r directory would delete the directory named “directory” and all files in it. This is a dangerous command that could easily delete a lot of important files, so be careful when using it. It won’t ask for confirmation.

mv – Move Files

The mv command moves a file to a new location. This is also the command you’ll use to rename files. For example, mv file newfile would take the file named “file” in the current directory and move it to the file named “newfile” in the current directory — renaming it, in other words.

Like with other commands, you can include full paths to move files to or from other directories. For example, the following command would take the file named “file” in the current directory and place it in the /home/howtogeek folder:
mv file /home/howtogeek

cp – Copy Files

The cp command works the same way as the mv command, except it copies the original files instead of moving them.
You can also do a recursive copy with cp -r. This copies a directory and all files inside it to a new location. For example, the following command places a copy of the /home/howtogeek/Downloads directory into the /home/chris directory:
cp -r /home/howtogeek/Downloads /home/chris

mkdir – Make Directories

The mkdir command makes a new directory. mkdir example will make a directory with the name “example” in the current directory.

ln – Create Links

The ln command creates links. The most commonly used type of link is probably the symbolic link, which you can create with ln -s.
For example, the following command creates a link to our Downloads folder on our Desktop:
ln -s /home/howtogeek/Downloads /home/howtogeek/Desktop

Check out our article on symbolic links for more information.

chmod – Change Permissions

chmod changes a file’s permissions. For example, chmod +x script.sh would add executable permissions to the file named script.sh in the current folder. chmod -x script.sh would remove executable permissions from that file.

Linux file permissions can be a bit complicated. Check out our guide to Linux file permissions for more in-depth information.

touch – Create Empty Files

The touch command creates an empty file. For example, touch example creates an empty file named “example” in the current directory.

mc – A Full File Manager

Midnight Commander is one of many fully featured file managers you can use from the Linux terminal. It isn’t installed by default on most distributions; here’s the command you’ll need to install it on Ubuntu:
sudo apt-get install mc
Once it’s installed, just run the mc command to launch it.

Use the arrow keys to select files and the Tab key to switch between panes. Press Alt-1 to see the help screen or Alt-2 to see the menu.

You can also use the mouse in Midnight Commander if your terminal environment has mouse support.

No comments:

Post a Comment