This article mainly selects the reader is necessary to learn the first 11 Linux commands, if the reader is not familiar with the virtual machine or cloud server under the actual operation, for developers, can master Linux to do some basic operations is necessary!

Without further ado, here are 11 Linux command-line hacks to make your life and work easier.

1. grep

$ grep "some string" fileCopy the code

The grep command searches each file, looks for patterns separated by newlines, and prints every line that matches the pattern.

Use the grep command to find all React keywords in the file:

The -i option enables a case-insensitive search for strings in a given file. It matches words like “REACT”, “REACT”, and “REACT”.

$ grep -i "REact" fileCopy the code

We can use the -c (count) flag to find the number of lines that match a given string/pattern.

$ grep -c "react" index.jsCopy the code

This is an interesting and educational cartoon about the grep command found online.

2. ls

$ lsCopy the code

Ls lists the files and directories in the current active path. If the pathname is a file, ls displays information on the file based on the requested options. If the pathname is a directory, ls displays information about the file and its subdirectories.

Use the ls command to display all files in the current directory:

You may have noticed that files are gray and folders are blue. This is to help us distinguish between folders and files.

3. pwd

$ pwdCopy the code

Print out the current working directory (PWD) :

The PWD command is a command line utility for printing the current working directory. Output prints the full system path to the current working directory to standard output. By default, the PWD command ignores symbolic links, although options can be used to show the full physical path to the current directory.

4. cat

$ cat somefile.jsCopy the code

The cat command has three functions related to text files:

  • Show them
  • Copy of merger
  • Create a new one

The most common use of cat is to read the contents of a file, and cat is often the most convenient command to read a file.

In the following example, the standard output of CAT is redirected to file2 using the output redirection operator (indicated by right-pointing Angle brackets) :

$ cat somefile > somefile2Copy the code

Create file with cat:

5. echo

$ echo "some text"Copy the code

Commands in Linux are used to display a line of text/string passed as an argument. This is a built-in command used mostly in Shell scripts and batch files to output status text to screens or files.

6. touch

$ touch somefileCopy the code

The touch command is used to create a file without any content. The touch command can be used when the user does not have data to store when creating a file.

Touch create new file:

How we use Touch to create files and CAT to see inside files. Since the newly created index2.js file is empty, cat does not return anything.

Here are the main differences between Cat and Touch:

  • Cat: Used to create a file containing content.
  • Touch: Creates a file without any content or empty files. Remember that files created using the touch command are empty. This command is useful when the user has no data to store when the file is created.

7. mkdir

$ mkdir some-directoryCopy the code

As you might expect, mkdir creates a new empty directory in the current active path. Use this command to create a new folder instead of clicking around in a text editor or GUI.

Create a new directory with mkdir:

7.1 the rm

$ rm someFileCopy the code

Rm stands for remove, which deletes files.

Delete files using rm command:

By default, the rm command does not delete directories. You need to pass the -RF flag to delete the directory.

$ rm -rf some-directoryCopy the code

Use the rm command to delete a directory:

7.2 rmdir

$ rmdir some-directoryCopy the code

Rmdir If there is no content in the directory, this command will delete the directory.

Delete an empty directory using rmdir:

8. tail

$ tail somefileCopy the code

The tail command reads the file and prints the last part of the file.

Print the last num line:

Tail This command is useful when viewing crash reports or previous history logs. The following is an example of using the tail command to query file logs.

[root@wupx ~]# [operation,,]# [http-nio-8111-exec-2] #[operation,]# [http-nio-8111-exec-2] c.g.s.c.i.ProjectIndexApi [ProjectIndexApi.java:537]: Query parameters: BaseQueryDTO{startDate=Tue Jan 01 08:00:00 CST 2019, endDate=Mon Dec 09 14:45:31 CST 2019, regionList=[], ProductCodeList = [apple]} the 20:35:40 2019-12-09, 024 - the INFO # # [sell,,,] the [HTTP - nio - 8111 - exec - 2] C.G.S.C.I.P rojectIndexApi [ProjectIndexApi.java:538]: Query interface: / products/projects/engineerType/count/range 20:35:40 2019-12-09, 085 - the INFO # # [sell,,,] the [HTTP - nio - 8111 - exec - 9] c.g.s.c.i.ProjectIndexApi [ProjectIndexApi.java:483]: Query parameters: BaseQueryDTO{startDate=Tue Jan 01 08:00:00 CST 2019, endDate=Mon Dec 09 14:45:31 CST 2019, regionList=[], ProductCodeList = [apple]} the 20:35:40 2019-12-09, 085 - the INFO # # [sell,,,] the [HTTP - nio - 8111 - exec - 9] C.G.S.C.I.P rojectIndexApi [ProjectIndexApi.java:484]: Query interface: 20:35:40 / products/projects/active/count 2019-12-09, 145 - the INFO # # [sell,,,] [HTTP - nio - 8111 - exec - 7) c.g.s.c.i.ProjectIndexApi [ProjectIndexApi.java:483]: Query parameters: BaseQueryDTO{startDate=Tue Jan 01 08:00:00 CST 2019, endDate=Mon Dec 09 14:45:31 CST 2019, regionList=[], ProductCodeList = [apple]} the 20:35:40 2019-12-09, 145 - the INFO # # [sell,,,] the [HTTP - nio - 8111 - exec] C.G.S.C.I.P rojectIndexApi [ProjectIndexApi.java:484]: Query interface: 20:35:40 / products/projects/active/count 2019-12-09, 246 - the INFO # # [sell,,,] [HTTP - nio - 8111 - exec - 8] c.g.s.c.i.ProjectIndexApi [ProjectIndexApi.java:483]: Query parameters: BaseQueryDTO{startDate=Tue Jan 01 08:00:00 CST 2019, endDate=Mon Dec 09 14:45:31 CST 2019, regionList=[], ProductCodeList = [apple]} the 20:35:40 2019-12-09, 246 - the INFO # # [sell,,,] the [HTTP - nio - 8111 - exec - 8] C.G.S.C.I.P rojectIndexApi [ProjectIndexApi.java:484]: Query interface: 20:35:40 / products/projects/active/count 2019-12-09, 313 - the INFO # # [sell,,,] [HTTP - nio - 8111 - exec - 5) c.g.s.c.i.ProjectIndexApi [ProjectIndexApi.java:483]: Query parameters: BaseQueryDTO{startDate=Tue Jan 01 08:00:00 CST 2019, endDate=Mon Dec 09 14:45:31 CST 2019, regionList=[], ProductCodeList = [apple]} the 20:35:40 2019-12-09, 313 - the INFO # # [sell,,,] the [HTTP - nio - 8111 - exec - 5] C.G.S.C.I.P rojectIndexApi [ProjectIndexApi.java:484]: Query interface: 20:35:40 / products/projects/active/count 2019-12-09, 401 - the INFO # # [sell,,,] [HTTP - nio - 8111 - exec - 4] c.g.s.c.i.ProjectIndexApi [ProjectIndexApi.java:306]: Query parameters: BaseQueryDTO{startDate=Tue Jan 01 08:00:00 CST 2019, endDate=Mon Dec 09 14:45:31 CST 2019, regionList=[], ProductCodeList = [apple]} the 20:35:40 2019-12-09, 401 - the INFO # # [sell,,,] the [HTTP - nio - 8111 - exec - 4] C.G.S.C.I.P rojectIndexApi [ProjectIndexApi.java:307]: Query interface: / products/projects/new/statics 20:35:40 2019-12-09, 490 - the INFO # # [sell,,,] the [HTTP - nio - 8111 - exec - 1) c.g.s.c.i.ProjectIndexApi [ProjectIndexApi.java:279]: Query parameters: BaseQueryDTO{startDate=Tue Jan 01 08:00:00 CST 2019, endDate=Mon Dec 09 14:45:31 CST 2019, regionList=[], ProductCodeList = [apple]} the 20:35:40 2019-12-09, 490 - the INFO # # [sell,,,] the [HTTP - nio - 8111 - exec - 1] C.G.S.C.I.P rojectIndexApi [ProjectIndexApi. Java: 280] : query interface: / products/projects/liveness/staticsCopy the code

9. wget

$ wget someurlCopy the code

GNU Wget is a free software package for retrieving files using HTTP, HTTPS, FTP, and FTPS (the most widely used Internet protocols). This is a non-interactive command line tool, so it can be easily invoked from scripts, CRON jobs, terminals that do not support X-Windows, and so on.

Wget gets information about web pages:

GNU Wget has a number of features that make it easy to retrieve large files or mirror entire Web or FTP sites, including:

  • You can use REST and RANGE to resume suspended downloads
  • You can use filename wildcards and recursively mirror directories
  • NLS based message files for multiple languages
  • Convert absolute links in downloaded documents to relative links so that downloaded documents can be linked to each other locally
  • Runs on most Unix-like operating systems as well as Microsoft Windows
  • HTTP proxy support
  • Support HTTP cookies
  • Support for persistent HTTP connections
  • Unattended/background operation
  • Use the local file timestamp to determine whether the document needs to be redownloaded when mirroring
  • GNU Wget is distributed under the GNU General Public License.

10. find

$ find path -name filenameCopy the code

The find command enables you to quickly find a file or directory. This is useful when you are working on large projects with hundreds of files and multiple directories.

Find all files named index.js:

Search for a specific type of file

The find command also allows you to search directories (and subdirectories) for files of the same type. For example, the following command will search for all files in the current working directory of.js.

$ find . -name "*.js"Copy the code

Find all.js files in the Components directory:

11. mv

$ mv somefile /to/some/other/pathCopy the code

The mv command moves a file or directory from one location to another. The mv command allows you to move a single file, multiple files, and directories.

Move some directory from component to utils directory:

conclusion

This article is about 11 common Linux commands. Thank you for reading and hopefully you learned something.