(pre) simple and fast use of your linux

Simple and fast use of your linux

1 start to contact linux

  • When you first came into contact with the Linux system, you came into contact with the mysterious command line and found that it was completely different from the graphical interface you normally contact. You were stunned. After reading this article, you can learn some simple common linux commands Convenient for your operation

2 start linux command

2.1 Open Directory

  • In Windows system, the most commonly used commands are open / copy / cut / delete

Open command: cd [dirName]

# Skip to / usr/bin /:
cd /usr/bin  

# Skip to your home directory:
>> cd ~

# Return to the upper directory:
>> cd ..

2.2 copying documents

  • In the project, we often need to copy files or data sets to our own directory. CP command is a command we must master

Copy command cp [options] source... directory

  • Description of common parameters:

    -f: Overwrite the existing target file without prompting.

    -r: If the given source file is a directory file, all subdirectories and files under the directory will be copied at this time.

# Pseudo code 
cp –r test/ newtest  

# The cp command uses the - r parameter to copy all files under package a to package B
cp -r /home/model/* /home/cp/model/

2.3 cutting documents

  • Similar to the above, it is generally understood as mv instruction

mv [options] source... directory

  • Description of common parameters:

    -f: Overwrite the existing target file without prompting.

    -n: Do not overwrite any existing files or directories.

# The target directory is consistent with the original directory. If a new file name is specified, the effect is to rename only.
mv  /home/model/a.txt   /home/model/b.txt    

# The target directory is inconsistent with the original directory, and no new file name is specified. The effect is to move only.
mv  /home/model/a.txt   /home/model/test/ 

# The target directory is consistent with the original directory, and a new file name is specified. The effect is: move + rename.
mv  /home/model/a.txt   /home/model/NIN_model/c.txt

2.4 deleting files

rm [options] name...

  • Description of common parameters:

    -f even if the original file attribute is set to read only, it can be deleted directly without confirming one by one.

    -r delete the directory and the following files one by one.

# Delete all files and directories in the current directory without prompting
rm  -rf  File name or directory to delete

# Delete file name nin py:
rm  -rf   NIN.py

# Delete the directory model. No matter whether there are subdirectories or files in the directory, you can delete it directly:
rm  -rf   model/

3 convenient use of linux commands

3.1 log in and refresh automatically bashrc

  • After the environment is configured, the file will be automatically generated in the general directory. If the function is missing, you can add it manually
  1. Create - > usr /. In user root directory bash_ Profile file
  2. Open the file and write the following code in the file
    if test -f .bashrc ; then 
    source .bashrc
    fi
    
  3. Save file

3.2 creating custom commands

  • Using custom commands can greatly simplify our operation For example, the following scenario

    After logging in to the terminal every time, we need to activate anaconda's virtual environment. We need to enter the following command

    > source activate tensorflow
    

    After customizing the command, we can use - evtf - instead of the above command

  • The specific steps are as follows

  1. Open in the user root directory - > usr / Bashrc file
  2. Open the file and write the following code in the file
    alias evtf='source activate tensorflow'
    alias evpt='source activate pytorch'
    
  3. Save file
  • Thus, we have realized two major functions. By inputting 'evtf' command, we are equivalent to inputting 'source activate tensorflow', which greatly simplifies our input

3.3 running python code in the background

  • In this section, we discuss the effect of 1 + 1 > 2 caused by the combination of different commands
  • How can I check the progress of the code running on the server when the code running on the server is interrupted for a long time?
  • (solution) use nohup/jobs/tail to realize single terminal multi python background tasks

3.3.1 tail

  • The tail command can be used to view the contents of the file. There is a common parameter - f, which is often used to view the changing log file.

tail -f filename

  • example
# To display nin Log and refresh in real time
tail -f NIN.log

# To display nin Contents of log
tail NIN.log

3.3.2 jobs

  • The jobs command is used to display all tasks running in the current terminal, including foreground tasks and background tasks When a task is running, we can switch the task to the background through Ctrl+Z, or forcibly end the task through Ctrl+C

  • Description of common parameters:

    -l: Displays the list of trusted information, including PID information

# Query the process running in the background of the current terminal
jobs

# Query the process running in the background of the current terminal and display its PID
jobs -l

3.3.3 fg bg

  • The fg and bg instructions are used to switch the jobs of the current terminal to the front and background, fg to switch the background process to the foreground, and bg to switch the foreground process to the background, but their usage is different. Please refer to the linux command guide for details

  • Description of common parameters:

    %n: Specify the process switching front and background corresponding to the job number

fg %n #Let the process n running in the background come to the foreground 
bg %n #Let the process n running in the foreground go to the background

# Switch background process 1 to foreground
fg %1

# Switch foreground process 2 to background
bg %2

3.3.4 nohup

  • The full English name of nohup is no hang up. It is used to run commands without hanging up in the background of the system. Exiting the terminal will not affect the operation of the program.

nohup Command [ Arg ... ] [ & ]

  • Parameter Description:
    Command: the command to execute.
    Arg: some parameters, you can specify the output file.
    &: let the command be executed in the background, and the command will still be executed after the terminal exits.
# Execute runoob.exe in the root directory in the background SH script and redirect input to runoob Log file
nohup /root/runoob.sh > runoob.log 2>&1 &

  • 2> & 1 explanation:

    Redirect standard error 2 to standard output & 1, which is then redirected to runoob Log file.

    0 – stdin (standard input)
    1 – stdout (standard output)
    2 – stderr (standard error output)

3.3.5 python

  • python is a command used to run py script. In fact, it is equivalent to calling the compiler. Therefore, pay attention to the problem of virtual environment and call the compiler in the appropriate environment, otherwise the running script will lack the environment and report an error
  • Please query the common parameters of python command by yourself, which will not be listed in this article
# Execute Nin in the background_ test. Py and redirect the information to nin_ In 5.log
nohup python -u NIN_test.py > NIN_5.log 2>&1 &

3.3.6 practical operation

  • An overall operation shows how to run long tasks through the server background
# Activate TF environment
@ubuntu:~$ evtf

# Activation succeeded with a tensorflow
(tensorflow)@ubuntu:~$ 

# Run py script in the background
(tensorflow)@ubuntu:~$ nohup python -u NIN_test.py > NIN_5.log 2>&1 &

# There is no prompt at the end of the command. You need to use jobs to confirm the operation
(tensorflow)@ubuntu:~$ jobs 

# The command is running. You can view the log
(tensorflow)@ubuntu:~$ tail -f NIN_5.log

# Confirm that the code is running normally. You can close the interrupt or switch to the background to start the next task

Added by akeane on Fri, 18 Feb 2022 01:32:22 +0200