Command Line Magic: Part II

Fun with rename:

1) To rename filenames with CAPS characters to lowercase:

rename ‘y/A-Z/a-z/’ *

2) To remove <spaces> from filenames:

rename ‘s/ /-/g’ *

3) To strip files with the .bak extension:

rename ‘s/\.bak$//’ *.bak

Free: The free command displays the amount of free and used memory in the system. For example, free -m gives the information using megabytes, which is probably most useful for current computers.

lsb_release -a: The lsb_release command with the -a option prints version information for the Linux release you’re running

sed: The sed (or Stream EDitor) command allows search and replace of a particular string in a file. For example, if you want to find the string “cat” and replace it with “dog” in a file named pets, type
sed s/cat/dog/g pets.

grep: An excellent tutorial.

Running commands sequentially in one command:
If you need to execute multiple commands in sequence but don’t need to pass output between them, there are two options based on whether or not you want the subsequent commands to run only if the previous commands succeed or not. If you want the commands to run one after the other regardless of whether or not preceding commands succeed, place a ; between the commands.

For example, if you want to get information about your hardware, you could run lspci ; lsusb which would output information on your PCI buses and USB devices in sequence.

However, if you need to conditionally run the commands based on whether the previous command has succeeded, insert && between commands. An example of this is building a program from source, which is traditionally done with ./configure, make, and make install.

The commands make and make install require that the previous commands have completed successfully, so you would use ./configure && make && sudo make install. Usually you’ll need an SUDO, for the last command, so make sure you’ve authenticated SUDO prior to running that command.

Learning the Linux Shell: A great tutorial.

Leave a Reply