This tutorial covers listing, filtering and counting of files (ls, grep, wc) Here we go. Let’s assume we are using GRIP (a GUI for converting Audio-CDs into MP3- or OGG-Files on harddisk. We want to explore the songs we have in our collection:
stw@laptop:~> cd Musik/ stw@laptop:~/Musik>
As you can see by the prompt, we are now in the music-directory. No, I do not pick up the KDE-Spelling - in Germany, we spell music like that .) The ‘cd’-command changes the working directory. Most shells display it in the prompt, so you always know where you are. In case you forget, simply type ‘pwd’ and it will ‘print (the) working directory’:
stw@laptop:~/Musik> pwd /home/stw/Musik
What songs are there in my collection? Let’s LiSt them: ‘ls’
stw@laptop:~/Musik> ls bob_dylan-the_bootleg_series_vol_1-01-hard_times_in_new_york_town.ogg [...] elton_john-the_superior_sound_of_elton_john-01-your_song.ogg [...] janis_joplin-pearl-01-move_over.ogg [...]
They pass by even faster than they do so in the charts! Too fast to read. So we pipe the output of ‘ls’ into the pager ‘less’ which prints a screenfull of lines and then waits for us to press SPACE to continue. Using the cursor-keys we can move up and down in the listing. Press ‘Q’ to quit.
stw@laptop:~/Musik> ls | less
How many tracks are there on that Bob Dylan album? ‘wc’ (word count) gives us statistics on words and (with -l appended) lines in a file. Since the m3u-playlists contain one line per track, we can use it:
stw@laptop:~/Musik> wc -l _PlayList-bob_dylan-the_bootleg_series_vol_1.m3u 22 _PlayList-bob_dylan-the_bootleg_series_vol_1.m3u
But there is more that we can do with ‘wc’ In that directory, there are OGG-Files with the music and m3u-files with the playlist for each album. So if I want to know how many songs I have I do:
stw@laptop:~/Musik> ls *.ogg | wc -l 511
This lists all files that end in ‘.ogg’ and pipes the list to ‘wc’, which counts the lines (=files) and tells me that i have 511 songs in my directory. As Feb, 14th approaches, we need to know if we have enough love-songs. ‘grep’ enters the scene:
stw@laptop:~/Musik> ls | grep -i love | wc -l 36
‘grep is case-sensitive, so I added the ‘-i’ switch to also match Love, LOVE and LovE. When Valentine’s Day finally comes, we want to play these in xmms. So we can prepare a playlist:
stw@laptop:~/Musik> ls | grep -i love >_Playlist4Valentine.m3u
We ‘redirect’ the output to a file. You can take a look at it with ‘cat’ or if it is too long for the screen with less: Try these:
stw@laptop:~/Musik> cat _Playlist4Valentine.m3u stw@laptop:~/Musik> cat _Playlist4Valentine.m3u | less
which is the same as:
stw@laptop:~/Musik> less _Playlist4Valentine.m3u
And when our loved-one arrives all we do is:
stw@laptop:~/Musik> xmms -p _Playlist4Valentine.m3u
The ‘-p’ tells xmms to start playing without waiting for us to push that button. As you can see, with only some commands and their combination, you can do many things quickly on the commandline. Just imagine picking all the love-songs from a dialog-box! These examples can be tuned to fit other purposes as well. Feel free to add your scenarios to this page!
Copyright (c) by the authors.
Prior to editing, authors agreed to license their contributions by the terms of the GPL.
See our licensing page for details.
Linux® is a registered trademark of Linus Torvalds.