Linux Programming

Acknowledgment to tutorialspoint.

https://www.tutorialspoint.com/unix/index.htm

You can use the cat command to see the content of a file and display the line numbers by using the -b option along with the cat command as follows

cat -b filename

You can use the wc command to get a count of the total number of lines, words, and characters contained in a file. 

wc filename

To make a copy of a file use the cp command.

cp source_file destination_file

To go in your last directory

cd -

To list the files in a directory

ls dirname

An important Unix concept is the environment, which is defined by environment variables. Some are set by the system, others by you, yet others by the shell, or any program that loads another program.

A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. For example, first we set a variable TEST and then we access its value using the echo command 

TEST="Unix Programming"
echo $TEST

It produces the following result.

Unix Programming

If we don't use the $ sign, it returns

TEST

Really clear a terminal instead of simply shifting previous output upwards when you run "clear" command.

tput reset

You can connect two commands together so that the output from one program becomes the input of the next program. Two or more commands connected in this way form a pipe. To make a pipe, put a vertical bar (|) on the command line between two commands. When a program takes its input from another program, it performs some operation on that input, and writes the result to the standard output. It is referred to as a filter.

The name "grep“ means "globally search for a regular expression and print all lines containing it”. For example,

ls -l | grep "lib"

There are various options which you can use along with the grep command

ls -l | grep -v "lib" # Prints all lines that do not match pattern.
ls -l | grep -c "lib" # Prints only the count of matching lines.
ls -l | grep -i "lib" # case insensitive search

View the history of a command

history|grep "main.py" # view all commands containing main.py

 Write the output of a command to a txt file

command >>logs.txt 2>&1 # errors and warnings included

 Disk usage of all users

sudo du -shc /home/*

Disk usage by subfolder

du directory-to-analyze/* -sh

Find the process on a specific port

sudo lsof -t -i:9001

 Get CPU information

cat /proc/cpuinfo  | grep 'name'| uniq

  

Add a sudo user

sudo adduser username
sudo usermod -aG sudo username

  

Delete a user

sudo deluser --remove-home userNameHere

  

 Create a virtual environment

conda create -n yourenvname python=3.6

   

 Remember the pwd in git

git config --global credential.helper store

 Remove files from git history

git pull
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch foo.mp4' --prune-empty --tag-name-filter cat -- --all
git push --force --all

 Running a command that does not terminate if you close the terminal

  

program-name & disown  # if you still want to see the output

  or

nohup program-name & # if you do not care about the output

   

Ubuntu change hostname command 

sudo vim /etc/hostname  # Delete the old name and setup new name
sudo vim /etc/hosts  # Replace any occurrence of the existing computer name with your new one.
sudo reboot

How to install SSH server in Ubuntu

sudo apt-get install openssh-server
sudo systemctl enable ssh
sudo systemctl start ssh

Disable Automatic Updates from Command Line: Edit /etc/apt/apt.conf.d/20auto-upgrades to disable automatic updates from the command line

sudo vim /etc/apt/apt.conf.d/20auto-upgrades

Once you have the file opened, switch off the Update-Package-Lists directive from 1 to 0.

How to install the NVIDIA drivers on Ubuntu 18.04

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
sudo apt install nvidia-driver-450

stress test for cpu

stress -c 8 -t 10

stress test for memory

stress -i 4 --vm 10 --vm-bytes 1G --vm-hang 100 --timeout 100s

  

Change password

sudo passwd user
原文地址:https://www.cnblogs.com/cxxszz/p/8573773.html