Skip to main content
  1. Posts/

Linux Tips and Tricks -- s2

··630 words·3 mins·
Table of Contents

A list of Linux command for common operations.

Generate fixed width numbers with leading zeros

We can use seq command to generate number of fixed width (padded with leading zeros). For example, to generate number 1 to 999, with width 4 and padded with leading zeros, we can use the following command:

seq -f "%04g" 1 999


Split large files into fixed size part

We can use split command to split large files into fixed size parts:

split [options] zip_file prefix

For example, to split into smaller size with each part exactly 4000m, we can use the following command:

split -d -b 4000m test

The -d option will ensure that numeric indexing is used for the split parts, test is the prefix we use for each part. The generated files are like:

test00, test01, test02, ..., test10, ...


Copy a directory to another directory and show progress

During copy of large files, it is nice to show the progress of copying., we can use rsync to copy a directory to another directory and show progress:

rsync -ah --progress src dest

Since rsync version 3.1.0, we can also use the following options:

rsync -ah --info=progress2 src dest

The progress shown is not the overall progress, it is just the progress for a single file. Nonetheless, it is better than nothing and shows that something is being copied.

Note in the above, there is not slash / after src, which makes sure that src is copied as dest/src with all its structures. If you want to copy all files and folders under src to dest and do not create src directory under dest, you can add a slash after src:

rsync -ah --progress src/ dest


Move all files and subdirectories to a directory except one

Suppose I am at a certain directory and want to move all the files and sub-directories to a directory except one file (test.jpg). We can use the following command to achieve what we want:

\ls | \grep -v test.jpg |xargs -I{} mv {} other_directory/

Note that in the above command we use backslashed version of ls and grep command to avoid any command alias, because the command alias may cause the failure of the above command.


zip all files under a directory without the directory itself?

If we have a test folder under which there are a lot of files and directories. If we use the following zip command to compress the directory:

zip -r test/

this will add one more level of folder structures. We get the following:

--> test
    --> test
        --> all files and folders under original test folder

The original test folder is also included in the zipped file. To avoid this, we can use the following command:

cd test && zip -r ../ .

If there are no directories under test or we do not care about directory structure under test, we can also use the -j option:

zip -j -r test/

The above command will flatten all files under test recursively into one directory, which may not be what you want, especally when there are same name filder under different child directories.



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