FlossDoc: 請協力 GPLv3 的中文翻譯工作。
Basic Command Line (Eng)
FlossDoc,自由中文開源知識庫
This article is aimed to teach people with zero knowledge on Unix/Linux about the basic Unix/Linux commands and the basic operation of command line. It focus on commands of basic file operation including ls, cd, cp, mv, and rm and providing part of the GNU/Linux administration knowledge required in the following objectives of LPI Level 1:
- 103.3 Perform basic file management (Weight: 4)
目录 |
Enter the command line environment
Depended on what environment your are in, there are many ways to enter the command line environment. Most new users choose to have their first try on GNU/Linux with a Graphical desktop environment. Running program known as Terminal Emulator (which often contains word “Terminal”, “xterm”, “console”, “konsole” or just “term” in their name) is the easiest way for those users to enter the command line environment. Most Debian or Ubuntu users can select Application → Accessories → Terminal to run GNOME Terminal. For Fedora, Red Hat or CentOS users, they can run it by selecting Application → System → Terminal.
If a text-based screen is showing with the word “login:”, the GNU/Linux is in a console mode and user need to enter their user name (login name) and password to login the system in order to enter the command line environment. (It is NORMAL that the screen show nothing even an asterisk (*) when you entering the password)
In short, if the screen show a dollar sign ($) or hash (#) on the left of the blinking cursor similar as following which known as prompt, you are in the command line environment:
guest@linux:~$ _
Understanding Prompt
People with experenice on the command prompt of MS-DOS or their operating system would not feel strange on prompt. The main purpose of prompt is telling you that the system is ready for you to entering command. There is some different on the prompt in different Unix or GNU/Linux distributions because of their default settings. For example, the prompt of Debian/Ubuntu is “guest@linux:~$”, the one of Fedora/CentOS/RedHat is “[guest@linux ~]$” and the one of SuSE Linux/OpenSUSE is “guest@linux:~>”. In general, the prompt usually show the login user name, machine hostname and current working directory and ended with a dollar ($), percentage (%) or hash (#) sign. We take the prompt of Debian/Ubuntu as an example:
guest@linux:~$
- guest - user name which allow you to know which user account you are logging in.
- linux - machine hostname which let you know the machine you are operating.
- ~ - Current working directory path which tell you which directory you are. (~ sign means home directory, the default directory when user login.)
- $ - Means what kind of user account you are logging in. Dollar (
$) sign means you are a normal user and hash (#) means you are the system administrator. (user root) In some Unix system, the prompt mean ended with percentage (%) sign which mean the user is using C Shell.
Basic file and directory operations
In general, using computers is an action of processing data. And data in computer is organized in a from of file. For example, writing a proposal is using a word processor (e.g. Micrsoft Word or OpenOffice.org) to create or edit a document file, listening a song is playing an audio file and browse a web page is using a WWW browser to retrieve an HTML file from the WWW server and display it. Indeed, all applications including word processor, music player, WWW browser and WWW server are program files. Therefore, the first thing we should learn is how to manage files under GNU/Linux.
Similar to Windows, Unix/Linux use a tree-like structure named as filesystem to organize files. The top of the whole filesystem is a directory named as root directory and donated by a slash sign (“/”, similar to the back slash “\” on Windows). The root directory can contains many files and directories and each directories can contains many files and directories too.
List files and change directory
The first command should be learned in Unix/Linux is ls. You can try to enter the command ls after the prompt and press ENTER key to see what will happen:
$ ls Desktop Documents Examples Music Public Pictures Templates Videos
The above screen means there are 8 files in the current directory including “Examples”, “Public”, “Pictures”, “Videos”, “Documents”, “Desktop”, “Templates” and “Music”.
Do not feel panic if nothing is show on the screen but a new line with the prompt as below:
$ ls $
This means that no file is found in the current directory. Let's change the current working directory to / (the root directory) with the command “cd /” (cd stand for change directory):
cd /
The prompt should changed to sometime like followings after enter the command:
guest@linux:/$ _
Type ls again the see the file list:
ls
The root directory should have some files that ls command should show sometime. ls is a short form of "list". You will find most command in Unix/Linux are in short form. For example, cp for copy and mv for move.
You can run a Unix/Linux command with some options to modify its behavior. For example, ls support an option “l” which list file as well as their information likes owner, size and last modified time. To run a command with a option, you must place a dash (“-”) before options and separate it and the command with at least one space. Taking ls as an example, the full command to run it with option “l” to see more information of files in current working directory is “ls -l”. Let type it in the command line:
ls -l
Here is a typical output of “ls -l”:
total 132 drwxr-xr-x 2 root root 4096 Aug 8 16:30 bin drwxr-xr-x 3 root root 4096 Oct 17 16:51 boot drwxr-xr-x 13 root root 13580 Oct 18 22:43 dev drwxr-xr-x 137 root root 12288 Oct 19 17:53 etc drwxr-xr-x 7 root root 4096 Oct 10 03:36 home drwxr-xr-x 16 root root 12288 Sep 27 14:33 lib drwx------ 2 root root 16384 Apr 26 00:55 lost+found drwxr-xr-x 3 root root 4096 Oct 18 18:18 media dr-xr-xr-x 168 root root 0 Oct 16 12:36 proc drwxr-xr-x 18 root root 4096 Oct 14 18:50 root drwxr-xr-x 2 root root 4096 Oct 17 16:49 sbin drwxrwxrwt 30 root root 36864 Oct 20 01:18 tmp drwxr-xr-x 12 root root 4096 Jun 3 15:09 usr drwxr-xr-x 16 root root 4096 May 18 09:36 var
The command “ls -l” show more information of each file. Files with lines begin with “d” like “bin” and “usr” are directory (folder). You can change your working directory to the directory “usr” with the following command:
cd usr
Same as before, you can use the ls command to list the files under the /usr directory (“/usr” means the directory named as “usr” under “/” which similar to “\usr” in Microsoft Windows and DOS.):
ls -l
You should see some directories like “bin”, “lib” and “share”, etc. You can change the working directory to the “bin” under “/usr” with the following command:
cd bin
Again, you can list the files with command ls. Besides, you can also show the full path of your current working directory with the command pwd (pwd stand for print working directory):
$ pwd /usr/bin
If you follow the above step correctly, you should see pwd show “/usr/bin” which is the full path of your current working directory. “/usr/bin” means a directory “bin” under the directory “/usr”.
You can list the files under other directory without change to it, you only need to add the path of the directory to the command ls as a parameter (argument). For example, you can list directory “/usr/share” with:
ls /usr/share ls -l /usr/share
home directory
If you type “cd” directly without any parameter, it will bring you back to your home directory. Try to following commands to check the path of your home directory:
cd pwd
Home directory is the default working directory every time you login a Unix/Linux system. It is one of the few directory in the whole Unix/Linux that owned by you and you can store files under it. It is similar as the idea of user profile in Windows platform - C:\Document and Settings\John in Windows XP and C:\User\John in Windows Vista/7.
All users in Unix/Linux have their own home directory. Usually, an normal user's home directory is a directory with the user's username as the name under the directory “/home”. For example, the home directory of user “john” is /home/john. And the home directory of user “mary” is /home/mary.
Because of security and recovering, the home directory of the administrator account “root”
Copy, move and remove files
To copy a file, you can use the command “cp”. The command need at least 2 arguments – the source and destination. For example, if you want to copy the file “/etc/passwd” (A file named as “passwd” under directory “/etc”) to current working directory as a new file named as “user-information”, you can use the command:
cp /etc/passwd user-information
If there is no any error message appeared and show the prompt immediately, it means the command is executed successfully. You can ls and see a new file “user-information” exists in your current directory.
To move files between directory, you can use the command mv. Same as cp, it need at least 2 arguments – the source and destination. For example, if you move the file “user-information” to the directory “/tmp”, you can use:
mv user-information /tmp
You can use the command mv to rename a file too. Indeed, renaming a file is same as move a file to another file in the same directory with different name. For example, we can copy the file “/etc/passwd” to the current directory and rename as “userprofile” with:
cp /etc/passwd passwd mv passwd userprofile
To remove a file, you can use the command rm:
rm /tmp/user-information userprofile
The above command remove both the file “/tmp/user-information” and “userprofile'”' at the same time.
ls - List directory contents
As mentioned in the previous section, ls is the command to list files in Unix/Linux (Similar to the command “dir /w” in MS-DOS/Windows) and the option l of it to make ls list files as well as their information (Similar to dir in MS-DOS/Windows)
ls long listing format
Here is a typical output of “ls -l” (long listing format of ls):
total 132 drwxr-xr-x 2 root root 4096 Aug 8 16:30 bin drwxr-xr-x 3 root root 4096 Oct 17 16:51 boot drwxr-xr-x 13 root root 13580 Oct 18 22:43 dev drwxr-xr-x 137 root root 12288 Oct 19 17:53 etc drwxr-xr-x 7 root root 4096 Oct 10 03:36 home drwxr-xr-x 16 root root 12288 Sep 27 14:33 lib drwx------ 2 root root 16384 Apr 26 00:55 lost+found drwxr-xr-x 3 root root 4096 Oct 18 18:18 media dr-xr-xr-x 168 root root 0 Oct 16 12:36 proc drwxr-xr-x 18 root root 4096 Oct 14 18:50 root drwxr-xr-x 2 root root 4096 Oct 17 16:49 sbin drwxrwxrwt 30 root root 36864 Oct 20 01:18 tmp drwxr-xr-x 12 root root 4096 Jun 3 15:09 usr drwxr-xr-x 16 root root 4096 May 18 09:36 var
The first line show the total block size of all the file listed. For the remain line, each line show the information of a file:
- File mode (
drwxr-xr-x) - Combined with the file type and the permission of a file:- File Type - The first character of file mode represent the type of the file. Dash (-) represent the file is an ordinary file likes a office document file or JPEG photo image. letter d represent the file is a directory. Here are some common file types in Unix/Linux:
- - : Ordinary file
- d : Directory
- l : Symbolic link (Also known as "soft link", similar to the Shortcut file, *.lnk, in Windows platform)
- c : Character device node. It is used as a point to access a hardware device without buffer, e.g. keyboard and mouse, etc.
- b : Block device node. It is used as a point to access a hardware device with buffer, e.g. hard disk, etc.
- p : Named pipe, also known as FIFO (First In First Out)
- s : Socket file
- Permission - The remain 9 character of file mode represent the permission of the file which indicate which user are allow to access or even edit the content of the file.
- File Type - The first character of file mode represent the type of the file. Dash (-) represent the file is an ordinary file likes a office document file or JPEG photo image. letter d represent the file is a directory. Here are some common file types in Unix/Linux:
- Link Count - Show how many file is point to the same i-node.
- User/Owner (
root) - Show which user own the file. - Group (
root) - Show which group does the file belong to. - Size (
4096) - Show the size of the file in byte. - Last Modified Time - Show the time that the file content was last modified.
- File Name - The name of the file. For symbolic link, both “
->” and the file path that the link pointed are show after the file name.
Hidden files in Unix/Linux
Similar to other platform, we can hide some files or directories for default file listing. In MS-DOS/Windows, switching on the Hidden or System attribute can hide a file. For Unix/Linux, use a file name started with dot (“.”) can make the file hidden by default. Let try to “ls” your home directory with and without the option “a” and see their difference:
$ ls Desktop Documents Examples Music Public Pictures Templates Videos $ ls -a . .config .gconf .gtk-bookmarks Public .thumbnails .. Desktop .gconfd .gtkrc-1.2-gnome2 Pictures .Trash .bash_history Documents .gnome .ICEauthority .recently-used .xsession-errors .bash_logout .esd_auth .gnome2 .metacity .recently-used.xbel .bash_profile Examples .gnome2_private .mozilla .ssh .bashrc .fontconfig .gstreamer-0.10 Music Templates
The ls -a should list more file and all the new file are started with “.”.
For GNU/Linux and Solaris, you can try the command “ls --all” too:
$ ls --all
Do you find any difference between the output of “ls -a” and “ls --all”? No. In fact, --all is only a long form of the option -a. Therefore, their output are SAME.
To list all files including hidden own as well as their information, you can use both l and a in the one ls command:
ls -l -a
The order of the options do not affect the output:
ls -a -l ls --all -l
You can even merge the two short form options into a single parameter:
ls -la ls -al
More options of ls
Option l and a are not the only two options of ls. Here are other command options of it:
- ls -r : List files in reverse order.
- ls -R : List files recursively. It will list all the files and directories under the one it listing.
- ls -l -t -a or ls -lta: List files including hidden one in long listing format and order by the last modified time.
- ls -l -S -a or ls -Sla: List files including hidden one in long listing format and order by the file size.
You may find that option r and option R of ls means different things. It indicates the option of Unix/Linux command are case sensitive too!
Directory “.” and “..”
You might aware that all directories in Unix/Linux filesystem contain two directories - “.” and “..” when you “ls -a”.
The directory “.” represents the directory you list itself. The directory “..” represents the
cp - Copy files and directories
The command need at least 2 arguments – the source and destination. For example, if you want to copy the file “/etc/passwd” (A file named as “passwd” under directory “/etc”) to current working directory as a new file named as “passwd” and you use the command “cp /etc/passwd”, the command will complain for missing the destination and fail:
$ cp /etc/passwd cp: missing destination file operand after `/etc/passwd' Try `cp --help' for more information. $
To copy the file “/etc/passwd” to current working directory as a new file named as “passwd”, the correct command is:
cp /etc/passwd passwd
OR
cp /etc/passwd .
mv - Move (rename) files
Same as command cp, command mv need at least 2 arguments - the source and destination too.
Summary
Commands
| Command | LPI Obj. | Description | Source of Name |
|---|---|---|---|
| ls | 1.103.3 | List directory contents | list |
| cat | 1.103.2 | Concatenate files and print on the standard output | concatenate |
| cd | - | Change the current working directory | change directory |
| cp | 1.103.3 | Copy files and directories | copy |
| mv | 1.103.3 | Move (rename) files | move |
| mkdir | 1.103.3 | Make directories | make directory |
| rm | 1.103.3 | Remove files or directories | remove |
| rmdir | 1.103.3 | Remove empty directories | remove directory |
| touch | 1.103.3 | Change file timestamp | touch |
References
See Also
External links
- Linux Planet Tutorial: The Linux CLI for Beginners, or, Fear Not the Linux Command Line! - Please Meet the Linux Terminal (Akkana Peck 2008-12-12)
- Linux Planet Tutorial: The Linux Command Shell For Beginners: What is the Shell? - So What's a "Shell", Anyway? (Akkana Peck 2008-12-22)

