Common Commands
cd /home Enter the ‘/home’ directory cd .. Return to the parent directory cd ../.. Return two levels up cd - Return to the last directory cp file1 file2 Copy file1 as file2 cp -a dir1 dir2 Copy a directory cp -a /tmp/dir1 . Copy a directory to the current working directory (. represents the current directory) ls List files in the directory ls -a Show hidden files ls -l Show detailed information ls -lrt Display files by time (l means detailed list, r means reverse order, t means sorted by time) pwd Show the working path mkdir dir1 Create the ‘dir1’ directory mkdir dir1 dir2 Create two directories at once mkdir -p /tmp/dir1/dir2 Create a directory tree mv dir1 dir2 Move/rename a directory rm -f file1 Delete ‘file1’ rm -rf dir1 Delete the ‘dir1’ directory and its subdirectory contents
View File Contents
cat file1 View the contents of a file from the first byte forward head -2 file1 View the first two lines of a file more file1 View the contents of a long file tac file1 View the contents of a file in reverse, starting from the last line tail -3 file1 View the last three lines of a file
Text Processing
grep str /tmp/test Search for “str” in the file ‘/tmp/test’ grep ^str /tmp/test Search for lines starting with “str” in the file ‘/tmp/test’ grep [0-9] /tmp/test Search for all lines containing digits in the file ‘/tmp/test’ grep str -r /tmp/* Search for “str” in the directory ‘/tmp’ and its subdirectories diff file1 file2 Find the differences between two files sdiff file1 file2 Display the differences between two files side by side
Searching
find / -name file1 Search for files and directories starting from ’/’ in the root filesystem find / -user user1 Search for files and directories owned by user ‘user1’ find /home/user1 -name *.bin Search for files ending in ‘.bin’ in the directory ’/ home/user1’ find /usr/bin -type f -atime +100 Find executable files not used in the past 100 days find /usr/bin -type f -mtime -10 Find files created or modified within the last 10 days locate .ps Find files ending in ‘.ps’; run the ‘updatedb’ command first find -name ’.[ch]’ | xargs grep -E ‘expr’ Search for ‘expr’ in all .c and .h files in the current directory and its subdirectories find -type f -print0 | xargs -r0 grep -F ‘expr’ Search for ‘expr’ in regular files in the current directory and its subdirectories find -maxdepth 1 -type f | xargs grep -F ‘expr’ Search for ‘expr’ in the current directory
Compression and Decompression
bzip2 file1 Compress file1 bunzip2 file1.bz2 Decompress file1.bz2 gzip file1 Compress file1 gzip -9 file1 Compress file1 to the maximum degree gunzip file1.gz Decompress file1.gz tar -cvf archive.tar file1 Package file1 into archive.tar (-c: create a compressed archive; -v: show all processes; -f: use the archive name, which is required and must be the last parameter) tar -cvf archive.tar file1 dir1 Package file1 and dir1 into archive.tar tar -tf archive.tar Display the contents of an archive tar -xvf archive.tar Extract an archive tar -xvf archive.tar -C /tmp Extract the archive to the /tmp directory zip file1.zip file1 Create a zip-format archive zip -r file1.zip file1 dir1 Compress files and directories into a zip-format archive unzip file1.zip Decompress a zip-format archive into the current directory unzip test.zip -d /tmp/ Decompress a zip-format archive into the /tmp directory
The yum Tool
yum -y install [package] Download and install an rpm package yum localinstall [package.rpm] Install an rpm package, using your own software repository to resolve all dependencies yum -y update Update all rpm packages installed on the current system yum update [package] Update an rpm package yum remove [package] Remove an rpm package yum list List all packages installed on the current system yum search [package] Search for packages in the rpm repository yum clean [package] Clear packages in the cache directory (/var/cache/yum) yum clean headers Delete all header files yum clean all Delete all cached packages and header files
Networking
ifconfig eth0 Display the configuration of an Ethernet card ifconfig eth0 192.168.1.1 netmask 255.255.255.0 Configure the IP address of the network card ifdown eth0 Disable the ‘eth0’ network device ifup eth0 Enable the ‘eth0’ network device iwconfig eth1 Display the configuration of a wireless network card iwlist scan Display wireless networks ip addr show Display the IP address of the network card
Miscellaneous
su - Switch to root privileges (different from su) shutdown -h now Shut down shutdown -r now Restart top List the Linux tasks using the most CPU resources (press q to quit) pstree Display processes as a tree diagram man ping View the reference manual (e.g., the ping command) passwd Change the password df -h Display disk usage cal -3 Display the calendar for the previous month, the current month, and the next month cal 10 1988 Display the calendar for a specified month and year date —date ‘1970-01-01 UTC 1427888888 seconds’ Convert a number of seconds relative to 1970-01-01 00
into a time
Common Shortcuts
In CentOS 6.4, you can set shortcuts via System -> Preferences -> Keyboard Shortcuts, as shown in the figure. For example, you can set the shortcut for running the terminal to Ctrl+Alt+T. Ctrl + u Delete characters from the cursor to the beginning of the line Ctrl + k Delete characters from the cursor to the end of the line Ctrl + c Cancel the command currently being entered on the line, equivalent to Ctrl + Break Ctrl + a Move the cursor to the beginning of the line (ahead of line), equivalent to the usual Home key Ctrl + e Move the cursor to the end of the line (end of line) Ctrl + f Move the cursor forward one character position Ctrl + b Move the cursor backward one character position Ctrl + l Clear the screen, equivalent to running the clear command Ctrl + r Display a : prompt and search for related history commands based on user input (reverse-i-search) Ctrl + w Delete from the position before the cursor to the beginning of the current word (word) Ctrl + t Swap the two characters before the cursor position Ctrl + y Paste the last deleted word Ctrl + Alt + d Show the desktop Alt + b Move the cursor backward to the previous word Alt + d Delete from the cursor position to the end of the current word Alt + F2 Run Alt + F4 Close the current window Alt + F9 Minimize the current window Alt + F10 Maximize the current window Alt + Tab Switch windows Alt + hold the left mouse button Move the window (or scroll the mouse wheel on the taskbar at the bottom)
[Middle mouse button] Paste highlighted text. Use the left mouse button to select text. Point the cursor to where you want to paste the text. Click the middle mouse button to paste.
[Tab] Command-line auto-completion. You can use this when using the shell prompt. Type the first few characters of a command or filename, then press the [Tab] key; it will automatically complete the command or show all commands matching the typed characters.
In the desktop or file manager, pressing / directly lets you type a location to open the file manager.
Quick search: pressing / directly in vi or Firefox enters search mode.
Website links and images can be dragged directly onto the desktop or into a directory, and they can be downloaded immediately.
Directly dragging a file from the file manager into the terminal gives you the full path name in the terminal.
Clicking the middle mouse button on the empty area of a scrollbar scrolls the screen to that position.
If you enjoyed this, leave a comment~