In this post, I list some of the often-used Linux command in my daily life.
Find all files under current folder with extension
find . -type f \( -iname "*.jpg" -o -iname "*.txt" \) -print
Got “argument list too long error” while deleting files
When I delete files directly using
rm some_dir/*, I get the error that
argument list is too long. We can use
find to delete files instead:
find . -type f -name "*.jpg" -delete
Add a prefix to each line of a file
It is trival to do this via sed:
sed -i.bak -e 's/^/<Pattern>/' test.txt
i.bak means to back up original file and create a new file.
How to use scp to transfer files between local and remote
Transfer remote file to local
scp -P <PORT> USER@remote_IP:/path/to/remote/file /local/directory
Transfer local file to remote
scp -P <PORT> /path/to/local/file USER@remote_IP:/path/to/remote_dir
You can rename the transfered file if you give a complete path to the file instead of just a remote directory.
Transfer remote folder to local
scp -P <PORT> -r USER@remote_IP:/path/to/remote/dir /path/to/local/dir
Transfer local folder to remote
scp -P <PORT> -r /path/to/local/folder USER@remote_IP:/path/to/remote/folder
When transferring folder from local to remote/from remote local, if the remote/local folder exists, the folder will be put as a child directory, otherwise, a new folder will be created with the name you give.
Download file using curl and rename
curl -o new_name -L file_link
-L tells curl to redirect.
How to show system reboot time?
last reboot to show system reboot time.
who -b can also show the system
last reboot time.
tar) files from text file?
The files we want to compress are written in a text file, e.g,
Each line in
img_list.txt represents a file path. How to compress all these
files to a single tar ball?
We can use the
-T option for tar:
-T, --files-from=FILE get names to extract or create from FILE
The command is:
tar zcvf images.tgz -T img_List.txt
Only search a pattern in certain filetypes?
Suppose we want to search
PATTERN in certain filetypes, for example,
grep -r -i PATTERN --include \*.py
Ripgrep is a fast searching tool,
commonly referred to as
rg. Using rg, it is easier to do this:
rg PATTERN -g "*.py"
- grep, but only certain file extensions
Randomly select N files from a folder?
find to find the certain files you want to select from, then use
shuf to randomly select the files.
find . -type f -name "*.jpg" -print | shuf -n 100
License CC BY-NC-ND 4.0