Record common system operations on Ubuntu
ln -s source_file target_file
ln -s source_folder target_folder
Make links to specific files:
ln -s source_folder/{1000..1025}.jpg target_folder
- copy files from 200001.jpg to 200100.jpg:
cp source_folder/20{0001..0100}.jpg target_folder
Use rsync:
rsync -aL source_folder target_folder
From local to remote:
- Folder:
scp -P port -r source_folder username@192.168.3.3:/target_folder
- File:
scp -P port source_file username@192.168.3.3:/target_file
From remote to local:
- Folder:
scp -P port -r username@192.168.3.3:/source_folder target_folder
- File:
scp -P port username@192.168.3.3:/source_file target_file
Copy source_folder
to target_folder
:
-
rsync -arP -e "ssh -p 2222" --max-size='100M' --exclude /exclude_folder1/ --exclude /exclude_folder2/ username@192.168.3.3:/source_folder/ target_folder/
- a: archive mode
- r: recursively
- P: partial and progress
- e: ssh and set port
- max-size: emit large files
- exclude: exclude some files or folders
du -h --threshold=1G | sort -h
- Or:
sudo du -h --threshold=1G | sort -h
Count files in current folder or some folder:
ls | wc -l
- Or:
ls -al | wc -l
Mount a remote filesystem to local and use it like local drives:
sshfs -p 2222 user@192.168.3.3:/remote_folder/ local_folder/
To unmount it, use fusermount
:
fusermount -u /path/to/sshfs/share
or Kill the process using pkill and then un mount the mounted folder path:
pkill -kill -f "sshfs" && umount /path/to/sshfs/share
pkill -kill -f "sshfs" && fusermount -u /path/to/sshfs/share
or search the mount progress:
cat /proc/mounts | grep mount_point
and then kill it:
cat /proc/mounts | grep mount_point | while read line; do
mount_point=$(echo $line | awk '{print $2}')
sudo umount -f "$mount_point" || sudo fusermount -u "$mount_point"
done
Use awk
:
To output the pid
of python process:
ps -ef | grep python | awk '{print $2}'
To kill the python process lauched by kongsea
:
ps -ef | grep python | grep kongsea | awk '{print $2}' | xargs kill
About awk
:
Output the items of a text line seperated by space or tab.
Output the first and fourth items of every line in log.txt
:
awk '{print $1,$4}' log.txt
Use ssh:
ssh -p port username@192.168.3.3
Use screen:
screen -S screen_name
- Run the program in the opened scene, then close the Terminal
screen -r screen_name
to restore the running program and look up the running status
For screen
commands, see screen
./shell.sh &
For example:
tensorboard --logdir tensor_dir &
Sometimes there are a huge number of images in a folder, you want to visualize an image from Terminal instead of opening the folder. Use:
eog file_path
We want to check some running process, for example, processes running using python. Use:
ps -ef | grep python
Search from contents of files.
grep -r content [file or folder]
Set Git user.name and user.email:
git config --global user.name "Kongsea"
git config --global user.email "Kongsea@gmail.com"
Look up the settings:
git config --global user.name
git config --global user.email
Use ssh-copy-id:
ssh-copy-id user@192.168.3.3
ssh-copy-id -p 23 user@192.168.3.3
Then you can use ssh
or scp
and don't need to input the password.
Use find:
find . -type d -print0 | while read -d '' -r dir; do
files=("$dir"/*)
printf "%5d files in directory %s\n" "${#files[@]}" "$dir"
done
or:
find -type d -maxdepth 1 | xargs -I {} sh -c "echo {}; ls -1 {} | wc -l"
or use:
du -a | cut -d/ -f2 | sort | uniq -c | sort -nr
Use hexdump:
Check 512 bytes from 800 bytes:
hexdump -C -s 800 -n 512 filename
or use:
tail -f somefile | hexdump -C
to check a file from end
You can use useradd
to add a user for Ubuntu.
However, in Ubuntu 18, the default login shell will be /bin/sh
. You will not get a standard prompt command. Instead, it will look like:
$
You can change the login shell by executing the command:
sudo usermod -s /bin/bash svn
Redis does not offer a way to delete some keys. We can use redis-cli
to bulk delete keys without blocking redis.
This command will delete all keys matching a pattern, say, starting with Kong
:
redis-cli --scan --pattern Kong* | xargs redis-cli del
If you are in redis 4.0 or above, you can use the unlink
command instead to delete keys in the background.
redis-cli --scan --pattern Kong* | xargs redis-cli unlink
Use clash without GUI to cross GFW:
vim clash/config.yaml
to add the proxies
and then:
sudo systemctl start clash.service
to start the service
At last, use proxychains
to cross the GFW.