Linux Command Line Encyclopedia

date displays the current time and date
cal displays the current month's calendar
df View the amount of free space on the disk
free shows the amount of free memory
pwd prints the current working directory
cd change directory
ls list folder contents
Absolute path: starts at the root directory
Relative path: starts from working directory.working directory..parent directory of working directory
cd - change directory to previous working directory
cd ~ change to home directory
ls
-l results are output in long mode
The -t option sorts files by modification time
--reverse results will be output in reverse order
-h display in a more friendly way
-a list all files including hidden files
file filename
Print a brief description of the contents of the file

less filename
View the content of the file, you can scroll forward and backward, press q to exit
Directory Reviews
/ The root directory, the origin of all things.
/bin contains the binaries necessary for the system to get up and running.
/boot contains the Linux kernel, the initial RAM disk image (for drivers needed at boot), and the boot loader.

Interesting files:
/boot/grub/grub.conf or menu.lst, is used to configure the boot loader.
/boot/vmlinuz, the Linux kernel.
/dev This is a special directory containing device nodes. "Everything is a file" also applies to devices. In this directory, the kernel maintains a list of all devices.
/etc This directory contains all system level configuration files.
It also contains a series of shell scripts that start each system service when the system starts. Any files in this directory should be readable text files.

Interesting files: While any file in the /etc directory is interesting, here are just a few that I've always liked:
/etc/crontab, which defines tasks to run automatically.
/etc/fstab, contains a list of storage devices, and their associated mount points.
/etc/passwd, contains a list of user accounts.
/home In the usual configuration environment, the system will assign a directory to each user under /home. Ordinary users can only write files in their own directory. This restriction protects the system from erroneous user activity.
/lib Contains shared library files used by core system programs. These files are similar to dynamic link libraries in Windows.
/lost+found This directory exists for every formatted partition or device that uses a Linux filesystem, such as the ext3 filesystem. This directory is used when partially restoring a corrupted filesystem. Unless the filesystem is truly corrupted, this directory will be an empty directory.
/media On modern Linux systems, the /media directory will contain mount points for removable media, such as USB drives, CD-ROMs, and so on. After these media are connected to the computer, they will be automatically mounted under this directory node.
/mnt On earlier Linux systems, the /mnt directory contained the mount point for removable media.
/opt This /opt directory is used to install "optional" software. This is mainly used to store commercial software products that may be installed on the system.
/proc The /proc directory is special. It's not really a file system in the sense of files stored on the hard drive. Instead, it's a virtual filesystem maintained by the Linux kernel. The files it contains are peepholes for the kernel. These files are readable, and they tell you how the kernel regulates the computer.
/root The home directory of the root account.
/sbin This directory contains the "system" binaries. They are programs that accomplish major system tasks and are usually reserved for superusers.
/tmp The /tmp directory is where temporary files created by various programs are stored. Some configurations cause this directory to be emptied every time the system is rebooted.
/usr On Linux systems, the /usr directory is probably the largest one. It contains all the programs and files that ordinary users need.
/usr/bin The /usr/bin directory contains executable programs installed by the system. Typically, this directory will contain many programs.
/usr/lib contains shared libraries used by programs in the /usr/bin directory.
/usr/local This /usr/local directory is the installation directory of programs that are included in non-system distributions, but are intended to be used by the system. Usually, programs compiled from source are installed in the /usr/local/bin directory. On a freshly installed Linux system, this directory will exist, but will be empty until the system administrator puts something in it.
/usr/sbin contains many hypervisors.
/usr/share The /usr/share directory contains many shared data used by programs in the /usr/bin directory. These include things like default profiles, icons, desktop backgrounds, audio files, and more.
/usr/share/doc Most packages installed on the system contain some documentation. In the /usr/share/doc directory, we can find documentation categorized by package.
/var With the exception of the /tmp and /home directories, the directories we've seen so far are relatively static, which means their contents don't change. The /var directory is where files that may need to be changed are stored. Various databases, spool files, user mail, etc., are located here.
/var/log This /var/log directory contains log files, records of various system activities. These files are very important and they should be monitored from time to time. One of the most important files is /var/log/messages. Note that for system security, on some systems you must be superuser to view these log files.
wildcard
* matches any number of characters (including zero or one)
? matches any character (excluding zero)
[characters] matches any character in the character set
[!characters] matches any character that is not in the character set
[[:class:]] matches any character in the specified character class

Common character categories:
[:alnum:] matches any letter or number
[:alpha:] matches any letter
[:digit:] matches any digit
[:lower:] matches any lowercase letter
[:upper:] matches any uppercase letter
mkdir directory create directory mkdir dir1 dir2 dir3 create three directories
cp file1 file2  
Copy the contents of file file1 to file file2.
If file2 already exists, the contents of file2 will be overwritten by the contents of file1. If file2 does not exist, file2 will be created.

cp -i file1 file2   
This command is the same as the command above, except that if the file file2 exists, the user is prompted for confirmation before the file file2 is overwritten.

cp file1 file2 dir1
Copy file1 and file2 to directory dir1. The directory dir1 must exist.

cp dir1 / * dir2  
Using a wildcard, all files in directory dir1 are copied to directory dir2. dir2 must already exist.

cp -r dir1 dir2
Copy the contents of directory dir1 to directory dir2. If the directory dir2 does not exist, create the directory dir2. After the operation is completed, the contents of the directory dir2 are the same as those in dir1.
If directory dir2 exists, directory dir1 (and its contents) will be copied to dir2.
mv file1 file2  
Move file1 to file2.
If file2 exists, its contents are overwritten with the contents of file1. If file2 does not exist, create file2. In each case, file1 no longer exists.

mv -i file1 file2   
This is the same option as above, except that if file2 exists, the user is prompted before file2 is overwritten.

mv file1 file2 dir1
Move file1 and file2 into directory dir1. dir1 must already exist.

mv dir1 dir2    
If the directory dir2 does not exist, create the directory dir2, move the contents of the directory dir1 to the directory dir2, and delete the directory dir1 at the same time.
If directory dir2 exists, move directory dir1 (and its contents) to directory dir2.
rm file1 silently deletes files
rm -i file1 has the same effect as the above command except that it prompts the user for confirmation before deleting the file.
rm -r file1 dir1 Delete file file1, directory dir1, and the contents of dir1.
rm -rf file1 dir1 Same as above, except that rm will continue if the file file1, or directory dir1, does not exist.
ln — create hard and symbolic links

ln file link creates a hard link
ln -s file line creates a soft link

Hard links Compared to the more modern symbolic links, hard links are the original Unix way of creating links. Each file will have a hard link by default, this hard link gives the file name. Every time we create a hard link, we create an extra directory entry for a file. Hard links have two important limitations:
1. A hard link cannot associate a file outside the file system where it is located. This is to say that a link cannot be associated with a file that is not on the same disk partition as the link itself.
2. A hard link cannot be associated with a directory.

When a hard link is removed, the link is removed, but the contents of the file itself remain.
Symbolic links are similar to Windows shortcuts, if you write to a symbolic link, the associated file is also written.
However, when you delete a symbolic link, only the link is deleted, not the file itself.
If you delete the file before the symlink, the link still exists, but doesn't point to anything. In this case, the link is called a bad link.
type command shows the type of command
which command shows the location of an executable program
ls --help --help option, this option is to display the syntax and option description supported by the command
man command View the reference manual for the command
whatis command displays a very concise command description
info command info info file is a tree structure, divided into various nodes, each contains a topic.
whatis command displays a very concise command description
alias name='command string' to alias the command
ls -l /usr/bin > ls-output.txt redirect output
less ls-output.txt View the output content
ls -l /usr/bin >> ls-output.txt uses the ">>" redirection character to append the content

A program can produce output on any of several numbered file streams.
While we've referred to the first three of these file streams as standard input, output, and error, the shell internally refers to them as file descriptors 0, 1, and 2, respectively.
The shell provides a notation for redirecting files using file descriptors. Since standard error is the same as file descriptor 2, we redirect standard error with this notation:
cat movie.mpeg.0* > movie.mpeg Append and connect eligible files
ls -l /usr/bin | less
The ability of a command to read data from standard input and pipe it to standard output is exploited by a shell feature called piping.

Add filter sort to the pipeline
ls /bin /usr/bin | sort | less
The wc (word count) command is used to display the number of lines, words and bytes contained in a file.
E.g:
wc ls-output.txt
7902 64566 503634 ls-output.txt
grep pattern [file...] find matching text

ls /bin /usr/bin | sort | uniq | grep zip case sensitive
ls /bin /usr/bin | sort | uniq | grep -i zip ignore case

grep “standard” standard.txt > grep.out 2>&1
Find the string standard in the file standard.txt, if found, redirect the search result to grep.out, if not, redirect the error message to grep.out.
head -n 5 ls-output.txt View the first five lines
tail -n 5 ls-output.txt View the last five lines
The tilde character ("~") has a special meaning.
When used at the beginning of a word, it expands to the home directory name of the specified user.
If no username is specified, it expands to the current user's home directory.

[me@linuxbox ~]$ echo ~foo
/home/foo
Arithmetic expression expansion uses this format:
$((expression))

[me@linuxbox ~]$ echo $((2 + 2))
4

Spaces are not important in arithmetic expressions, and expressions can be nested.
[me@linuxbox ~] echo $(($((5**2)) * 3))
75

With it, you can create multiple text strings from a pattern containing curly braces.
[me@linuxbox ~]$ echo Front-{A,B,C}-Back
Front-A-Back Front-B-Back Front-C-Back

[me@linuxbox ~]$ echo Number_{1..5}
Number_1  Number_2  Number_3  Number_4  Number_5

[me@linuxbox ~]$ echo {Z..A}
Z Y X W V U T S R Q P O N M L K J I H G F E D C B A

Nested braces:
[me@linuxbox ~]$ echo a{A{1,2},B{3,4}}b
aA1b aA2b aB3b aB4b

Batch create directories named in the form of numeric "year-month"
[me@linuxbox ~]$ mkdir Pics
[me@linuxbox ~]$ cd Pics
[me@linuxbox Pics]$ mkdir {2007..2009}-0{1..9} {2007..2009}-{10..12}
[me@linuxbox Pics]$ ls
2007-01 2007-07 2008-01 2008-07 2009-01 2009-07
2007-02 2007-08 2008-02 2008-08 2009-02 2009-08
2007-03 2007-09 2008-03 2008-09 2009-03 2009-09
2007-04 2007-10 2008-04 2008-10 2009-04 2009-10
2007-05 2007-11 2008-05 2008-11 2009-05 2009-11
2007-06 2007-12 2008-06 2008-12 2009-06 2009-12
Command substitution allows us to use the output of a command as an expansion pattern:
[me@linuxbox ~]$ echo $(ls)
Desktop Documents ls-output.txt Music Pictures Public Templates
Videos

bash also supports this syntax. It uses backticks instead of dollar signs and parentheses:
[me@linuxbox ~]$ ls -l `which cp`
-rwxr-xr-x 1 root root 71516 2007-12-05 08:58 /bin/cp

Remember that within double quotes, parameter expansion, arithmetic expression expansion, and command substitution still work:
[me@linuxbox ~]$ echo "$USER $((2+2)) $(cal)"
me 4    February 2008
Su Mo Tu We Th Fr Sa


The following example compares no quotes, double quotes, and single quotes:
[me@linuxbox ~]$ echo text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER
text /home/me/ls-output.txt a b foo 4 me
[me@linuxbox ~]$ echo "text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER"
text ~/*.txt   {a,b} foo 4 me
[me@linuxbox ~]$ echo 'text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER'
text ~/*.txt  {a,b} $(echo foo) $((2+2)) $USER

backslash escape character
[me@linuxbox ~]$ mv bad\&filename good_filename
View historical information
history | less

history command execution
!! Repeats the last command executed. It might be easier to press the up arrow key and enter key.
!number Repeats the command on line number in the history list.
!string Repeats commands starting with this string in the recent history list.
!?string Repeats commands in the recent history list that contain this string.
ps - report the current process snapshot
top - show tasks
jobs - list active jobs
bg - put a task into the background to execute
fg - bring a task to the foreground for execution
kill - send a signal to a process
killall - kills the process with the specified name
shutdown - shutdown or restart the system

Process status:
R is running. This means, the process is running or ready to run.
S is sleeping. The process is not running, but is waiting for an event, for example, a key press or a network packet.
D Uninterruptible sleep. The process is waiting for I/O, say, I/O to a disk drive.
T stopped. The process has been instructed to stop running. More on that later.
Z A dead or "zombie" process. This is a terminated child process, but its parent process hasn't emptied it yet. (The parent process did not delete the child process from the process table)
< A high priority process. This may grant a process more important resources, giving it more CPU time. This property of a process is called niceness.
A process with a high priority is said to be less nice because it takes up more CPU time, leaving little time for other processes.
N low priority process. A low-priority process (a "good" process) only gets processor time when other high-priority processes are serviced.

Another popular combination of options is "aux" (without the leading "-" character). This will give us more information:
[me@linuxbox ~]$ ps aux
USER   PID  %CPU  %MEM     VSZ    RSS  TTY   STAT   START   TIME  COMMAND
root     1   0.0   0.0    2136    644  ?     Ss     Mar05   0:31  init
root     2   0.0   0.0       0      0  ?     S<     Mar05   0:00  [kt]

Use the top command to dynamically view processes
Background execution: After the program command, add the "&" character
[me@linuxbox ~]$ xlogo &
[1] 28236

Background commands are immune to keyboard input and cannot be interrupted, use fg to forward commands to the front.
After the fg command, follow a percent sign and the task number (called jobspec).
[me@linuxbox ~]$ jobs
[1]+ Running        xlogo &
[me@linuxbox ~]$ fg %1
xlogo

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324766019&siteId=291194637