Mar 20, 2012

Simple command-line tricks


It has been a while since we had a column dedicated to useful command line tips. This week we will cover some simple, but often useful, command line tricks.

This first command will play all of the MP3 files in my Music directory, including all of the files found in sub-directories. It uses the find program to locate all MP3 files in the music directory and passes them off to the mplayer multimedia program to be played. The "-iname" flag tells find to ignore the case of the file name so that myfile.mp3 will be treated the same as MyFile.MP3. The braces, {}, toward the end of the command represent a file located by the find program which will be handed over to mplayer.
find ~/Music -iname "*".mp3 -exec mplayer {} \;
Sometimes people send me archives or CDs which contain files with improper access permissions. The first of the following two commands locates all folders in the current directory and grants my user full access to them. The second command locates all files in the current directory (and sub-directories) and provides read and write access to them without making them executable. The "-type" flag lets us select whether we are operating on a (d)irectory or on a (f)ile. The chmod program then applies the proper permissions to each directory and file.
find . -type d -exec chmod 700 {} \;
find . -type f -exec chmod 600 {} \;
When we first install our operating system we sometimes need to change the system date. Or, if our computer clocks don't automatically change to accommodate daylight saving time we might need to make adjustments. From the command line this is fairly straight forward. The following command sets the current system time to noon.
date -s "12:00"
Please note we will need to have administrator access to change the date and time. The next command tells the machine to change the clock to March 24, 12:30.
date -s "Mar 24 12:30"
They say timing is everything and it is useful to be able to schedule commands to be run at a later point in time. The following command will cause the sound file alarm.wav to play at 5pm.
echo mplayer alarm.wav | at 17:00
The at command can also be used to run scripts at a given time as in the following example where we run my_script at 11:40.
at 11:40 < my_script
Last, but not least, this is a very handy command to have when an application needs to be terminated. Let's say we have the VLC multimedia player running and, for some reason, we need to close it. Rather than look up its process ID and terminate the program with the kill command we can use a program called pkill to shut down the process by name.
pkill vlc
The command line is very flexible and often useful, not only for complex tasks and processing information, but also for common day-to-day activities.

Source Distrowatch.com

No comments:

Post a Comment