×
Community Blog The 20 Most Common Commands to Help Beginners Get Started with Linux

The 20 Most Common Commands to Help Beginners Get Started with Linux

This article gives a comprehensive list of common commands for the Linux operating system.

Preface

People who have used Linux know that there are many commands in Linux. However, learning so many Linux commands is unnecessary if you master the most commonly used ones. Everyone uses Linux for different purposes, so their commonly used commands are very different. I mainly use Linux for C, C++, and Shell programming. Therefore, my commonly used commands are different from the commands of Linux system administrators. I’d like to summarize them here for your convenience. Now, let’s talk about my most commonly used Linux commands.

1. cd Command

This is a very basic command that people often need to use. It is used to switch the current directory. Its parameter is the path of the directory to be switched to, which can be an absolute path or a relative path. For example, cd /root/Documents # switches to the directory /root/Documents. cd ./path # switches to the path directory under the current directory, and “.” indicates the current directory. cd ../path # switches to the path directory in the upper-layer directory, and “..” indicates the directory at the upper layer.

2. ls Command

This is a very useful command to view files and directories. It means list. It has many parameters. Some of my commonly used parameters are listed below:

  • -l: List long data strings, including file attributes and permission data
  • -a: List all files, including hidden files (files start with “.”)
  • -d: Only list the directory itself instead of the file data of the directory
  • -h: List the file capacity in a readable way (like GB and KB)
  • -R: List all contents, including the contents of the subdirectory (recursively). In other words, all files under the directory are displayed.

Note: These parameters can also be used in combination. Here are two examples:

  • ls -l # lists the data files and directories in the current directory in the form of a long data string.
  • ls -lR # lists all files in the current directory in the form of a long data string.

3. grep Command

This command is often used to analyze the information in one line. If there is any information we need, the line is displayed. This command is usually used together with the pipeline command to filter and process the output of some commands. Its simple syntax is grep [-acinv] [-color=auto] ‘search string’ filename. Its common parameters are listed below:

  • -a: Search binary files for data as text files
  • -c: Calculate the number of times of ‘search string’
  • i: Ignore case sensitivity
  • v: Reverse selection

Display the line that does not have the content of the ‘search string’

For example, retrieve the line that contains MANPATH from the file /etc/man.config and add the color to the found keyword through grep -color = auto ‘MANPATH’ /etc/man.config

Output the ls -l Output Containing the Letter File (Case Insensitive)

ls -l | grep -i file

4. find Command

find is a very powerful command based on search. Its use is relatively complicated, and it has many parameters. So, they are classified and listed here. The basic syntax is listed below:

find [PATH] [option] [action]

Time-Related Parameters:

  • -mtime n: “n” is a number, meaning files that have been changed "the day" before n days
  • -mtime +n: List files that have been changed before n days. (The n-th day is not included.)
  • -mtime -n: List files that have been changed within n days. (The n-th day is included.)
  • -newer file: List the file names newer than the file. For example, find /root -mtime 0 finds the files that have changed within today in the current directory.

Parameters related to the user or user group name:

  • -user name: List the files whose owner is the name
  • -group name: List the files whose user group is the name
  • -uid n: List files with the user ID of the owner as n
  • -gid n: List files with the user group ID as n. For example, find /home/ljianhui -user ljianhui finds the files whose owner is ljianhui in the /home/ljianhui directory.

Parameters related to file permissions and name:

  • -name filename: Find the files named filename
  • -size [+-]SIZE: Find the files larger (+) or smaller (-) than SIZE
  • -type TYPE: Find files of TYPE type

The values of TYPE mainly include general files (f), device files (b, c), directories (d), connection files (l), socket (s), and FIFO pipeline files (p).

  • -perm mode: Find files with file permissions exactly equal to mode, and mode is represented by numbers, such as 0755.
  • -perm -mode: Find files that must include all the mode permission, and mode is represented by numbers.
  • -perm +mode: Find files whose file permissions include the permissions of any mode, and mode is represented by numbers. For example, find / -name passwd finds the files named passwd. find . -perm 0755 finds the files with the file permission of 0755 in the current directory. find . -size +12k finds the files larger than 12 KB in the current directory.

5. cp Command

This command is used to copy files. Multiple files can be copied to the same directory at one time. The following part lists its common parameters:

  • -a: Copy the attributes along with the file
  • -p: Copy the attributes along with the file together but in the default method. It is similar to -a and is often used for backup.
  • -i: If the target file already exists, the operation will be asked before overwriting.
  • -r: The recursive and persistent copy for copying directories
  • -u: Only copy the file when there is a difference between the target file and the source file

For example: cp -a file1 file2 copies file1 together with its all attributes to file2.

cp file1 file2 file3 dir copies file1, file2, and file3 to the dir directory.

6. mv Command

This command is used to move or rename files and directories. The common parameters are listed below:

  • -f: It means force. If the target file already exists, overwrite directly without asking.
  • -i: If the target file already exists, it will ask whether to overwrite.
  • -u: If the target file already exists and is older than the source file, it will update.

Note: This command can move one or more files into one folder at a time, but the last target file must be "directory."

For example, mv file1 file2 file3 dir moves file1, file2, and file3 to the dir directory.

mv file1 file2 renames file1 to file2.

7. rm Command

This command is used to remove files or directories. Its common parameters are listed below:

  • -f: It means force. Ignore nonexistent files without sending warning messages.
  • -i: It is interactive mode. Before removing a file, the user will be asked whether to perform the operation.
  • -r: It is recursive removal, which is most commonly used to remove directories. It is a very dangerous parameter. For example, rm -i file removes files, and you will be asked whether to perform this operation before removal.

rm -fr dir forcibly removes all files in the dir directory.

8. ps Command

This command is used to select and output the running status of the process at a certain point in time. It means process. Its commonly used parameters are listed below:

  • -A: All processes are displayed.
  • -a: All processes not related to the terminal are displayed.
  • -u: Related processes of valid users
  • -x: Generally used with the parameter a to list more complete information
  • -l: List PID information in a longer and more detailed way

We only need to remember the common command parameter collocation that the ps command uses. They are not many:

  • ps aux views all process data in the system.
  • ps ax views all processes unrelated to the terminal.
  • Ps -lA views all process data in the system.
  • ps axjf # views the status of a part of the process tree.

9. kill Command

This command is used to send a signal to a job (%jobnumber) or a PID (number). It is usually used together with the ps and jobs commands. Its basic syntax is listed below:

kill -signal PID signal

Common parameters of signal are listed below:

Note: The first number is the signal code, and the code can be used to replace the corresponding signal. 1: SIGHUP. Start the terminated process. 2: SIGINT. It is equivalent to ctrl + c, interrupting the running of a program. 9: SIGKILL. Force the interruption for the running of a process. 15: SIGTERM. Terminate the process in normal termination process mode. 17: SIGSTOP. It is equivalent to ctrl + z, pausing the running of a process. For example: If you want to terminate the first background process in a normal termination process mode, run the jobs command to check the first worker process that runs in the background. Run kill -SIGTERM %1 to change the process with an ID of PID again. PID can be filtered out using the ps command, the pipeline command, and the grep command.

kill -SIGHUP PID

10. killall Command

This command is used to send a signal to a process started by a command. Its general syntax is listed below:

killall [-iIe] [command name]

Its parameters are listed below:

  • -i: It means interactive. When performing deletion, the system will ask the user.
  • -e: It means that the following command name must be consistent, but the command name cannot exceed 15 characters.
  • -I: The command name can ignore the case sensitivity. For example: killall -SIGHUP syslogd restarts syslogd.

11. file Command

This command is used to determine the basic data of the file after the file command because the type of files in Linux is not based on the suffix. So, this command is very useful and easy to use. The basic syntax is listed:

file filename, for example: file ./test

12. tar Command

This command is used to package the file instead of compressing it by default. If the corresponding parameters are specified, it also calls the corresponding compression program, such as gzip and bzip, to perform compression and decompression. Its common parameters are listed below:

  • -c: Create a new package file
  • -t: View which file names are included in the package file
  • -x: Unpack or decompress. It can be paired with -C (uppercase) to specify the directory for decompression.

Note: -c, -t, and -x cannot appear in the same command at the same time.

  • -j: Perform compression and decompression via bzip2
  • -z: Perform compression and decompression via gzip
  • -v: During the compression and decompression process, the name of the file being processed is displayed.
  • -f filename: Filename is the name of the file to be processed.
  • -C dir: Specify the directory dir for compression and decompression.

The explanations above can be confusing, but usually, we only need to remember the following three commands:

  1. For Compression: tar -jcv -f filename.tar.bz2 names of files and directories to be processed.
  2. For Query: tar -jtv -f filename.tar.bz2
  3. For Decompression: tar -jxv -f filename.tar.bz2 –C directory for decompression

Note: The file name does not have to end with tar.bz2. It is mainly to explain that the compression program used here is bzip2.

13. cat Command

This command is used to view the content of a text file. It is followed by the name of the file to be viewed. Generally, it can be used with the parameter pipeline (more or less) to view data page by page. For example: cat text | less views the content of the text file. Note: This command can also be replaced by less text.

14. chgrp Command

This command is used to change the user group to which a file belongs. It is easy to use and works like this:

chgrp [-R] dirname/filename

  • -R: It makes recursive changes to all files and subdirectories continuously. For example: chgrp users
  • -R ./dir recursively changes the user group of all files in the dir directory and all files in the subdirectories to users.

15. chown Command

This command is used to change the owner of the file. It is used the same way as the chgrp command, except that the modified file attributes are different. It is no longer described in detail.

16. chmod Command

This command is used to change the permissions of files. The general usage is listed below:

chmod [-R] xyz file or directory

  • -R: It makes recursive and continuous changes.

That means all the files in the subdirectory are also changed. chmod can also use u (user), g (group), o (other), a (all), + (addition), - (deletion), and = (set) paired with rwx to change the permissions of the file.

For example, the chmod 0755 file changes the file permission of the file to -rxwr-xr-x.

The chmod g+w file adds the write permission for the user group to the file permission of the file.

18. vim Command

This command is mainly used for text editing. It takes one or more file names as parameters. It opens the file if the file exists and creates a file with the file name if the file does not exist.

19. gcc Command

This command is very important for a person that develops C programs with Linux. It is used to compile the source program file of the C language into an executable program. Since many parameters of g++ are very similar to gcc, only the gcc parameters are introduced here. The common parameters are listed below:

  • -o: It means output. It is used to specify the file name of an executable file.
  • -c: It is used to generate the target file based on the source file (.o). It prevents the compiler from creating a complete program.
  • -I: It adds the search path for header files during compilation.
  • -L: It adds the search path for the static connection library during compilation.
  • -S: It generates assembly code files based on source files.
  • -lm: It indicates the function library named libm.a in the directory of the standard library.
  • -lpthread: It connects the thread library of NPTL implementation.
  • -std=: It is used to specify the version of the C language used.

For Example: gcc -o test test.c -lm -std=c99 compiles the source file test.c into an executable program test according to the c99 standard. gcc -S test.c converts the source file test.c into the corresponding assembly program source file test.s.

20. time Command

This command is used to estimate the execution time of a command (program). It is very simple to use. The word time needs to be added before the command, for example: time ./process time ps aux. At the end of the program or command, it outputs three times: user for user CPU time. It means the user CPU time taken to complete command execution, which is the total command execution time the in user state and system for system CPU time. It means the system CPU time taken to complete command execution, which is the total command execution time in core state and real for real-time. It means the time from the execution of a command line to the termination of the command.

Note: The sum of the user CPU time and the system CPU time is the CPU time, which is the total time that the command occupies CPU for execution. The real-time is greater than the CPU time because Linux is a multi-task operating system. In other words, when executing a command, the system also has to deal with other tasks. Another problem that needs attention is that even if the same command is executed each time, the time spent is also different, which is related to the system operation.

This article was originally published on the Programmer Bai Nannan official WeChat Account.

Disclaimer: The views expressed herein are for reference only and don't necessarily represent the official views of Alibaba Cloud.

0 1 1
Share on

Alibaba Cloud Community

875 posts | 198 followers

You may also like

Comments

Alibaba Cloud Community

875 posts | 198 followers

Related Products