The commands in this article are all run in Cmder. Click me to get the Cmder Amway website to view the specific meanings of the commands and parameters: explainshell

Abbreviations of common words and commands

English translation Commands/Parameters
file file
make making mk
move dynamic mv
remove delete rm
copy copy cp
list The list of ls
recursive recursive -r
link link ln
find find find
echo The echo echo
touch touch touch
change change CD in the c
directory Directory/folder D in the CD
force mandatory -f

Two, practical tips

role The command shortcuts
Interrupt a command Ctrl + C
Clearing the command line clear
Copy the arguments of the previous command Alt + .
See the history history

User directory

On Windows, you can’t fiddle with the directories in disk C, such as Program Files, Program Files (x86), Windows, etc. (you will find that these directories cannot even be renamed). You can only operate the Users directory. The Users directory has an Administrator directory (or whatever name you give your computer), which is the user directory.

In the user directory, you can play around. So the first thing you do when you open the command line is go to the user directory (or some other directory, such as the project directory).

cd /c/Users/Administrator
Copy the code

The above command input is too troublesome, change a simple:

cd ~	/c/Users/Administrator
Copy the code

Four, add, delete, change and check

1. Add: Create a file

  • Create a file
touch 1.txt
Copy the code
  • Create a file and write to it
echo hi > 1.txt		# Single greater than sign indicates coverage
echo hihi >> 1.txt	The double greater-than sign indicates the append
echo -e "hello\nworld" >> 1.txt	 # Enter multiple lines of content, requiring arguments -e, requiring double quotation marks, \n indicating a newline
Copy the code
  • Create multiple files at the same time
touch 1.txt 2.txt
Copy the code
  • Create a directory
mkdir a
mkdir -p a/b/c		Create a multi-level directory
Copy the code
  • Create multiple directories simultaneously
mkdir a b c
mkdir -p a/b/c d/e/f	Create multiple multilevel directories
Copy the code
  • Copy the file
cp 1.txt 2.txt
Copy the code
  • Copy directory
cp -r a b	See if there are other directories in the directory. If there are, continue to look, if there are not, copy
Copy the code

2. Delete: Deletes files

  • Delete the file
rm 1.txt
Copy the code
  • Delete the directory
rm -r a		Check to see if there are other directories in the directory. If there are, continue to check. If there are not, delete
Copy the code
  • Forcibly delete a directory with content
rm -rf a
Check to see if there are other directories in the directory. If there are, continue to check. If there are not, delete
# -f, force, force
Copy the code

Do not run rm -rf /, or you will be useless!!

3. Modify: Modify a file or directory

  • Modifying file Contents
start 1.txt		# Windows
open 1.txt		# Mac
Copy the code

The above command opens a file or directory using the default program. If code is available in your Cmder, open it with code.

code 1.txt	Use VSCode to open the file
Copy the code

Once the file is opened, you can modify the file contents.

  • Append file contents
echo "hello world" >> 1.txt
Copy the code
  • Empty file
echo "" > 1.txt		# Single greater than sign, indicating coverage
Copy the code
  • Renames a file or directory
mv 1.txt 2.txt		# rename 1.txt to 2.txt
mv a b			Rename directory A to directory B
Copy the code
  • Move files or directories
mv 1.txt a		# move 1.txt to a directory
Copy the code

Q: Rename and move commands are mv, how to distinguish?

See if the destination path exists. To exist is to move, to not exist is to rename. For example, mv 1.txt a:

  • If directory A exists, move 1.txt to a
  • If directory A does not exist, rename 1.txt to a
  • Change the last update time of a file
touch 1.txt
Copy the code

Q: What does touch do?

File does not exist, touch is a new file; The file exists, touch is to change the last update time of the file.

4. Search: View files or directories

  • View the absolute path of the current directory
pwd
Copy the code
  • View the current directory
ls		# Do not show hidden files
ls -la		# Display hidden files and detailed file information
Copy the code
  • View the contents of a specified directory
Ls path# sample
ls a/b/c	# check the contents of a directory b directory C directory
Copy the code
  • Viewing file Contents
The cat path# view all

# sample
cat 1.txt	
Copy the code
Less path# View only part of the file
		Press J/K or up/down to scroll up and down to see the content
		# Press Q to exit
		If a section is selected, press ESC to deselect it and then press Q to exit

# sample
less 1.txt	
Copy the code
The head pathThe first 10 lines are displayed by defaultHead-n 14 PathDisplay the first 14 lines
Copy the code
Tail pathThe last 10 lines are displayed by defaultTail -n 14 PathDisplay the last 14 lines
Copy the code

5. View the command help manual

What if I can’t remember all these commands? Use the following command to view the help manual.

Man command# orThe command -help

# orThe command -help | less	Click J/K or scroll up/down to view
			# | pipe
Copy the code

The help manual above is too long, do not want to read or do not understand what to do? Install TLDR and view common command documents.

npm i -g tldr

# or
yarn global add tldr
Copy the code
# use
tldr ls		# check the common usage of ls
tldr cp		# check cp usage
Copy the code

6. Combination of commands

The first thing to know is that a command can succeed or fail. If successful, there is no prompt on the command line; If the command fails, an error message is displayed.

You can also use echo $? To view the return value of the previous command. Returns 0 on success and non-0 on failure.

1. && operation

Effect: After a command succeeds, the next command is executed.

touch 1.txt && rm 1.txt && echosuccessfulCopy the code

Create 1.txt and delete 1.txt, then print “success”.

rm 2.txt && echosuccessfulCopy the code

The above code, because 2.txt does not exist, failed to delete 2.txt, so it will not print “success”.

(2); operation

Function: Executes the next command regardless of success or failure.

rm 2.txt; echosuccessfulCopy the code

The above code, since 2.txt does not exist, fails to remove 2.txt, but still executes the next command, printing “success”.

Write a script to create the project

Steps:

  1. Create a file without the suffix
Touch with one clickCopy the code
  1. Open the file and write the command
mkdir aaa
cd aaa
touch index.html
touch style.css
touch main.js

echo -e "
      \n

Title

"
>> index.html Copy the code
  1. Add executable permissions to the file (Windows does not have this concept)
Chmod +x in one clickCopy the code

Add executable permission command is chmod +x file, +x is to add executable permission.

  1. Execute the file
./ One click#./ Be sure

# orSh one clickCopy the code

After executing the “One-click” file, you will see that a directory named AAA has been created with files such as index.html, style. CSS, and main.js.

Above, only one AAA project can be created. What if you want to create BBB, CCC, DDD projects?

Two methods, one is to change the code, change the file aaa to BBB, CCC, DDD, and then run again; The second is to use parameters that tell the file what project I want to create.

Simply change the code in the file to the following, and all other steps remain unchanged.

mkdir The $1
cd The $1
touch index.html
touch style.css
touch main.js

echo -e "
      \n

Title

"
>> index.html Copy the code

To execute the file, do something like this:

CCC./ DDD./ CCC./ DDD# orSh BBB SH CCC SH DDDCopy the code

Above, you can create different projects by attaching different parameters to the executing files.

Eight, other

1. shebang

A shebang is a comment line that specifies what program is used to execute the current script.

Try the one-click solution above:

Run the current script with sh
#! /usr/bin/env sh

mkdir The $1
cd The $1
touch index.html
touch style.css
touch main.js

echo -e "
      \n

Title

"
>> index.html Copy the code

Since sh is specified in the script to run the script, you do not need to add sh when running the script. You can run aaa directly with one click. But since./ is necessary, our final order is:

./ One click to get aaaCopy the code

2. Turn files into commands

The command above seems to be the same as before adding shebang. Is there a cleaner way to execute the file?

Of course there is. You can use the file name as a command simply by putting the path of an executable file into path. That is, the file becomes a command.

Complete BBB with one clickCopy the code

Thus, these can be executed commands, in fact, are files.

For example, check the directory where the ls command (file) resides:

where ls

# or
which ls
Copy the code




Reprint please indicate the source!!