Skip to main content
  1. Posts/

Linux Tips and Tricks -- s1

··513 words·3 mins·
Table of Contents

In this post, I list 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

Ref:

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

Ref:

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.

Ref:

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?
#

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

Ref:

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

Ref:

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
#

Ripgrep is a fast searching tool, commonly referred to as rg. Using rg, it is easier to do this:

rg PATTERN -g "*.py"

Ref:

Randomly select N files from a folder?
#

First use 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

Ref:

Related

Fix Nvidia Apt Repository Public Key Error
·241 words·2 mins
Fuzzy-switching Tmux Sessions with Ease
··365 words·2 mins
Install LLVM/Clangd from Source on CentOS 7
··445 words·3 mins