Learning to learn to use Linux can be challenging, as everything from its reputation to general look can be intimidating. Having never used it before, instead using Windows both at home and for school I always saw Linux as something that was in the realm of true veteran software engineers and computer programmers.
During my first few weeks at Setfive I’ve had the chance to begin learning Linux and I have found it to be a useful and powerful tool. After learning some common and not so common commands I’ve really started to appreciate the flexibility and ease of use of the command line.
Here are 10 easy Linux “one liners” that allow us to accomplish some everyday tasks in a simple and efficient manner.
1. Sort a file
$ sort myfile.txt
This will sort the given file in numerical and alphabetical order:
Here a file, num, is being sorted alphanumerically, resulting in a sorted list.
2. Delete duplicate lines within a file
$ sort myfile.txt | uniq
This small addition to the sort command will not only sort the file but will remove any duplicates found within the file:
Here we can see the same num file being sorted, however this time the duplicates are being removed leaving us only with 1 of each entry.
3. Convert .mp3 and .wav files
$ ffmpeg -i input.mp3 output.wav
With this example we convert an .mp3 file to a .wav file, and it can be done the other way as well converting .wav to .mp3.
$ ffmpeg -i input.wav output.mp3
4. Recursively creating a directory structure
$ mkdir -p new/directory/structure/example
Using mkdir we are able to create subdirectories, however with the -p option we can tell it to create the subdirectories but also any parent directories that don’t already exist. Allowing an entire directory tree to be created with one line:
Here we can see an entire directory tree “/new/directory/example/setfive” being created.
5. Extract specific pages from a PDF
$ pdfjam pdf1.pdf 2-4 -o 2.pdf
Using the pdfjam command we are able to take pages 2, 3 and 4 from the pdf pdf1.pdf and save them in a separate file, here called 2.pdf.
6. Create a thumbnail image for a PDF
$ convert -thumbnail x80 file.pdf[0] thumb.png
Here we are using convert to create a thumbnail for a pdf, with the [0] indicating that the thumbnail will be made using the first page of the PDF file.
7. Make all file names lowercase
(Assuming you have a bash like shell)
$ for i in *; do mv "$i" "${i,,}"; done
This command will loop through the directory and use the built in case modification extension that comes with Bash to change all the file names to be lowercase.
8. Create an animated gif
$ convert -delay 20 -loop 0 *.jpg myimage.gif
This can be accomplished using the imagemagick package, which can be installed with “sudo apt-get install imagemagick”. This allows for full image manipulation including conversion and editing.
9. Create a file with some text
$ echo "a new string" >> file
It appends the string into the file, followed by a newline, if the newline is wanted then by adding the -n flag after echo will append the string without the following newline.
10. Split up a file
$ split -b 50MB verylargemovie.mp4
Split will break up a large file automatically to whatever sizes you need. Here we’re breaking up our big movie file into 50MB chunks.
Bonus: Rerun the last command, replacing part of the command
This allows for an easy method to rerun a command with the swapped string which can be useful with long commands where finding and altering a single string by hand would be tedious.
Using the convert example from above:
$ convert -delay 20 -loop 0 *.jpg myimage.gif $ ^myimage^newimage convert -delay 20 -loop 0 *.png newimage.gif
Will re-run the command and create an image named “newimage.gif”