Welcome to our third post about simple and (perhaps) useful tricks to use with Linux Terminals.
Check out the first and second posts in the series if you missed them!
Unless defined differently, these tricks will work in standard bash with any Terminal base snapshot, or in most of the modern Linux distributions.
Tip 1: Reading Linux man pages, in color
The boring and monochrome way to read a man page is just by using the man command, but you can also install most. Most is a little utility to make the man pages easy to read by adding some colors to them.
Let's install it:
(Ubuntu)
(CentOS)
You can also set up your default PAGER to use most when calling man. If you use bash, you can add it to your .bashrc file to make the changes permanent.
Tip 2: Split a large file into small pieces
I remember having to do this many years ago, when still dealing with floppy disks. You can split a file into smaller pieces of a certain size by using the split command.
Check out this example:
-a 2 means the suffix length is two characters (aa, ab, ac... zz)
-b 10M means the size of the chunk is 10M
bigfile.txt is the name of the file to be divided
small_chunk- is the prefix used in the names of the chunks or parts
Now, if you want to recombine the file, you can just use the cat command:
Easy, right?
Tip 3: Merge or split PDF files directly in your Terminal
We will use the convert command line utility, which is part of the Imagemagick package. In order to use convert you need first to install the imagemagick package by executing:
(Ubuntu)
(Centos)
Now that we have Imagemagick, we can merge and split files.
Merging PDF files:
This command will merge part1.pdf and part2.pdf into one single file called merged.pdf.
You can also merge a subset of pages instead of all pages in the input files:
In this example, we merge pages from 0 to 5 of file1.pdf and pages 3, 7, 8 and 9 of file.pdf into a single merged.pdf file.
Splitting a PDF file:
This takes certain pages of the input PDF file and copies them into different output files:
In this example we divide the file in two by taking certain subsets of pages from the same file and saving them in two different output files.
Tip 4: Hide a command from the bash command line history
You may want to do this for different reasons, for instance, when using plain password as commands argument (this is a bad practice, of course!).
To hide a command from the bash history, just leave a blank space at the beginning of the line.
Tip 5: Renaming files in bulk
We will use the rename command to change the name of several files in a batch. Imagine that we have a folder with several hundred photo files, downloaded from your camera. They have names like DSC-XXX.jpg where X is an entire number.
's/DSC/birthday-party/' is a perl-compatible regular expression.
If you want to replace ALL occurrences of a pattern in a file name, you must specify a global replace. For example, if you want to replace all spaces in the file names with null (remove all spaces):
Now, suppose you want to replace all occurrences of spaces, underscores, and round brackets with the dash character - :
Tip 6: Sharing your bash history across multiple sessions
By default, your bash session history is saved when your session ends. This was just fine when people worked with only one session at a time. Nowadays it's easy to work with multiple terminal sessions by just opening a new window or, in the case of Terminal, by opening a new tab.
In order to enable this feature, open your ~.bashrc file and add the lines in below:
The first statement is to avoid duplicates in your command history.
The second statement appends the history to the file, instead of rewriting it every time.
Finally, the third line will set the PROMPT_COMMAND variable with the proper history commands to save the history file, clean the in-memory history stack, and reload it from the file again. This will happen each time the prompt is shown.
Tip 7: List all files that were modified by a command
Sometimes you run commands that you don't know entirely and afterwards you're not sure what was modified in a certain directory of your system.
To avoid that problem, you can execute your command like this:
Where <command> is what you want to execute.
This simply takes the date (in nanoseconds resolution) and saves its value in a variable, then it executes the command and search the directory for the files that were modified since the date stored in the variable.
Note that this only works on the current directory and its subpaths, but you can change it by extending the find scope to something different from ..
See this trivial example:
Note that I'm executing two commands in this case (mkdir and touch). You can use as many commands as you want, surrounded by D="$(date "+%F %T.%N")"; and find . -newermt "$D"
Tip 8: Show current connections and their details
You can use lsof -i -r to show all connections and their details in realtime.
Tip 9: Generate a random password
In the line above, we take a random string from the /dev/urandom special device, just leaving the permitted characters and cutting it down to 16 characters.
You can change the value 16 to another number depending on how long your password needs to be.
Tip 10: Use pv to actively monitor data sent through a pipe
With pv you can watch your piped operations by getting information like time elapsed, percentage of completion, throughput rate, and ETA.Let's install it:
(Ubuntu)
(CentOS, adding the EPEL repository)
And some usage examples:
Piping the contents of a file into another file
Getting info about a tar and gzip process
See how fast your Terminal copies from /dev/zero to /dev/null
For more information please check the pv man pages.
Final notes
I hope you've enjoyed this list of tricks. All of them will work in Terminals.
Feel free to contribute with your own tricks in the comments section!