In this post, I summarize some of the often-used Linux command in my daily life.

Find all files under current folder with extension jpg or txt?

find . -type f \( -iname "*.jpg" -o -iname "*.txt" \) -print

References

Got “argument list too long error” while deleting files

find . -type f -name "*.jpg" -delete

References

Add a prefix to each line of a file

sed -i.bak -e 's/^/<Pattern>/' test.txt

i.bak means to back up original file and create a new file.

References

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 directory.

Transfer remote folder to local

scp -P <PORT> -r USER@remote_IP:/path/to/remote/dir /path/to/local/dir0

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, else, 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?

Use last reboot to show system reboot time. who -b can also show the system last reboot time.

References

compress (using tar) files from text file?

The files we want to compress are written in a text file, e.g, img_list.txt. 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

References

Only search a pattern in certain filetypes?

Suppose we want to search PATTERN in certain filetypes, for example, *.py files.

Use grep

grep -r -i PATTERN --include \*.py

Use ripgrep

rg PATTERN -g "*.py"

References

Randomly select N files from a folder and write the output to a file?

First use find to find the certain files you want to select from and use shuf to randomly select the files.

find . -type f -name "*.jpg" -print|shuf -n 100 > random_files.txt

References