Linux

Examples and comments about some Linux commands and applications.


alias
Redefine a command.
alias rm="rm -i" asks for confirmation when deleting a file.
Include as a line in .bashrc

Assembler
The compiler gcc with the option -S converts a C code into assembler. Its object code is got with as -o and it can be transformed into executable code with gcc.

gcc -S program.c creates the assembler text code program.s and as -o program.o program.s generates the object code program.o. If it does not contain a main procedure the executable program.out can be got with ld -o program.out program.o, otherwise gcc -o program.out program.o does the job.

See GNU Assembler for a Hello, world! example.

cal
Make a calendar.

cal 2050 -m > cal.txt the ASCII file cal.txt is a printable monthly (usual) calendar of 2050.

cal shows the calendar of the current month.


chmod
Set permissions.

chmod u+w myfile.txt allows the user to write.

chmod u=rw,go= myfile.txt allows the user to read and write and denies the access to everybody else.

chmod u=rw,go=r myfile.txt allows the user to read and write and denies the access to everybody else.

chmod a-x myfile.txt denies everybody to run the file.

chmod a-x myfile.txt allows everybody to run the file.


Compress, pack, extract and unpack (gzip, bzip2, tar, zip)

gzip myfile.txt compress myfile.txt as myfile.txt.gz

gzip -d myfile.txt.gz uncompress myfile.txt.gz as myfile.txt

bzip2 myfile.txt compress myfile.txt as myfile.txt.bz2

tar xvjf myfile.tar.bz2 uncompress and unpack myfile.tar.bz2

tar -cvf myfile.tar ./dir pack ./dir into myfile.tar

tar -zcvf myfile.tar.gz ./dir the same as before compressing the result

tar -xvf myfile.tar unpack myfile.tar

tar -xvf myfile.tar -C dir/ the same as before copying the resulting files into dir/

tar -xzvf myfile.tar.gz uncompress and unpack myfile.tar.gz

tar -jxvf file.tar.bz2 the same for myfile.tar.bz2

zip myfile.zip myfile.txt uncompress myfile.zip as myfile.txt

zip myfile.zip uncompress myfile.zip

zip -r myfile.zip dir/ compress dir/ as myfile.zip

zip -e myfile.zip myfile.txt compress myfile.txt asking for password


convert
This is a powerful tool depending on ImageMagick with a lot of options.

convert img*.png all.pdf pastes img*.png in the new PDF file all.pdf

convert -size 100x100 img.jpg -resize 100x100 imgp.jpg resizes (if necessary) with maximum width and height 100px. It seems convenient to write twice the dimensions.

convert -size 614x585 input.png -resize 100x95 output.png plain resizing.

convert input.gif -coalesce temp.gif followed by convert -size 614x585 temp.gif -resize 100x95 output.gif to resize an animated gif. The file temp.gif is temporal and can be deleted.

convert -monochrome image.jpg new_image.jpg converts a color image into a low resoluton B/W image.

convert -density 200 in.pdf[0-0] out.jpg passes the first page of in.pdf to out.jpg. In general [a-b] is the range of pages, starting by 0. Here -density means the quality. The default quality may be very low.

convert 'vid:*.jpg' list.pdf creates list.pdf a visual image directory of the jpg images.

convert xc:none -page A4 blank.pdf creates blank.pdf containing a blank page. This is useful in combination with pdftk.

See also mogrify. In the website of ImageMagick there are many examples.

cp for massive copy

find ./dir/ -type f -iname '*.tex' -exec cp {} ./backup/ \; copies every .tex file in ./dir or its subfolders to ./backup/. Changing ./dir this is done for every directory and putting . also for hidden directories.


crontab
A cron is a file containing commmands to be executed with a certain periodicity. The format of each line of the file is (taken from wikipedia):
.---------------- minute (0 - 59) 
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
| | | .------- month (1 - 12) OR jan,feb,mar,apr ...
| | | | .----- day of week (0 - 7) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
| | | | |
* * * * * command to be executed
For scripts it is important to write the complete path and do not use relative paths.

One example of a cron with one line is: 20 11 * * * /mypath/managecounters.py >/dev/null 2>&1 After activating it it will run /mypath/managecounters.py everyday at 11:20 not storing any output. Changing >/dev/null 2>&1 by myfile.txt the output is redirected to a file.

crontab mycron activates the cron mycron

crontab -l shows the list of crons. With only a cron as in the first example one could get something like:
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (micron installed on Fri Jul 30 11:13:32 2010)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
20 11 * * * /path/managecounters >/dev/null 2>&1


crontab -r removes the crontab file.


du
Size of directory trees and files.

du -hs /dir shows the size of /dir. Here -s means displaying only the total amount (without separating individual files) and -h is "human readable". It can be replaced by -b bytes (default) or -k kilobytes.

du -sk .[!.]* *| sort -n (taken from here) shows the size of all files and subfolders including the hidden ones and they appear ordered by size.

du -a /dir shows the size of all files and subfolders of /dir.

du /dir shows the size of all subfolders of /dir.

du -c /dir shows the size of all subfolders of /dir and displays the total size.

du --max-depth=2 /dir shows the size of all subfolders until depth 2 (subfolders and subsubfolders).

du -a --exclude='*.avi' /dir shows the size of all files and subfolders of /dir excluding the files *.avi.

du --max-depth=1 -c ./dir | sort -n > list.txt stores in list.txt the size of direct subfolders of ./dir ordered by size. It is convenient to figure out where we are spending memory.

du -sh ./* | sort -rh | head -n 10 shows the top ten biggest files or folders in the current directory.


ffmpeg to capture the desktop
See the examples.

ffmpeg -f x11grab -r 25 -s 1024x768 -i :0.0+100,200 output.flv captures the rectangule starting at (100,200) of size 1024x768 with framerate 25.

ffmpeg -f x11grab -r 25 -s 1024x768 -i :0.0 -t 10 output.flv records during 10 seconds.

ffmpeg -f x11grab -s `xdpyinfo | grep -i dimensions: | sed 's/[^0-9]*pixels.*(.*).*//' | sed 's/[^0-9x]*//'` -r 25 -i :0.0 -sameq output.avi grabs the full desktop getting automatically the size of the screen from xdpyinfo".

ffmpeg -video_size 1920x1080 -framerate 30 -f x11grab -i :0.0 -c:v libx264 -qp 0 -preset ultrafast capture.mkv better quality thanks to -qp 0".

It is convenient to put the launching konsole out of the captured region. Another possibility is to specify the time and launch the command with Alt+F2. The documentation includes many examples. See also this.

ffmpeg to change the audio or video format
Essentially it is enough to precede the input file with -i.

ffmpeg -i input.wav -f mp3 output.mp3 converts input.wav into output.mp3 forcing the format mp3.

ffmpeg -i input.mpeg output.flv

ffmpeg -i input.mpeg -sameq output.flv preserves the same quality.

ffmpeg -i input.mpeg -r 20 output.mpeg changes the fps to 20.

ffmpeg -i input.mpeg -s 320x240 output.mpeg changes the size to 320x240.

The documentation includes many examples. See also this.

ffmpeg to create a video from images
See the example.

ffmpeg -start_number 7 -i imag%d.png -vcodec mpeg4 output.avi creates the video file output.avi from the images imag7.jpg, imag8.jpg, etc.

ffmpeg -framerate 40 -start_number 24 -i imag%d.png -vcodec mpeg4 -s 720x480 output.avi starts from imag24.jpg with framerate 40 and targetting for a resolution !720x480".

It seems better to adjust the quality with the option -crf in the library libx264 using a shell script like this:
#!/bin/bash

frn=24
qual=23
#51 is the worst quality,  0 is lossless. Default=23. Normal range=[18,28]

rm output.avi
rm output.mp4

ffmpeg  -start_number 0 -framerate $frn  -i ./images/fig%d.png  -c:v libx264  -crf $qual  -c:a aac -strict -2    output.avi

ffmpeg -i output.avi  output.mp4
du -h output*

Depending on the choice of qual, the size in megas of output.avi for a clip of one minute (1440 images at 24fps) was 18 -> 5.4, 20 -> 4.3, 22 -> 3.4, 24 -> 2.8, 26 -> 2.4, 2.8 -> 2.1.
The documentation includes many examples. See also this. *

ffmpeg to extract audio
See the example.

ffmpeg -i input.mp4 -f mp3 -ab 192000 -vn output.mp3

The documentation includes many examples. See also this.

fold
Adjust the number of characters per line of a text file.
fold -s -w80 mysource.txt > mydestination.txt copies mysource.txt into mydestination.txt adjusting the number of columns to 80.
The option -s indicates to break at spaces. This is of course impossible if the length of a word is longer than the length of the line.

Gimp: Save-all-layers
There is a script sg-save-all-layers.scm by Saul Goode to save all layers as different image files. It must be downloaded in .gimp-2.8/scripts. Running gipm in the menu File it will appear the new option. The symbols ~ indicate the digits to numerate the image files storing the layers.
If gimp was already running, perhaps it is necessary to refresh the scripts in Filters>Script-Fu>Refresh scripts.

iconv
See the example.

iconv -f UTF-8 -t ISO-8859-1 input.tex > output.tex the file originally encoded with utf8 (the usual codification in Linux systems) is re-encoded as western European.


Information about the system
df -h Disk usage
dmesg Logs of the system (long)
fdisk -l Hard disks
ifconfig -a (or ip a) IP and network interface card
lscpu
CPU
lspci
PCI ports
lsusb USB ports
uname -a
Linux version (32 bits, i686, 64 bits x86_64)
getconf LONG_BIT 32 or 64 bits
The files /proc/cpuinfo and /proc/meminfo that can be seen with cat, contain more detailed information about the CPU and the memory.

KDE Plasma keyboard shortcuts
ALT+F2 Application launcher
ALT+Tab Navigate through windows
CTRL+ALT+Shift+Del Logout without confirmation
CTRL+Esc System activity
CTRL+F1-F4 Switch to desktop 1-4
CTRL+F7 Show all the windows of current application
CTRL+F8 Show all desktops on current desktop
CTRL+F9 Show all windows of present desktop on current desktop
CTRL+F10 Show all windows of every desktop on current desktop
CTRL+F12 Show the desktop
Shift+ALT+F12 Disable temporarily the effects
Taken mainly from the KDE documentation.
Important: In non American keyboards some combinations involving the special key (the windows key in PCs) can be very difficult to revert, for instance zoom. To recover temporarily the panel use Shift+Alt+F12. In the case of zoom, edit .config/kwinrc setting [Effect-Zoom] InitialZoom=1.

kill
One of the strongest commands to kill a process is kill -9 followed by the number of the process that can be checked with ps -A. Another possibility to kill all the instances is using killall or pkill followed by the name. A graphic variant of kill is xkill, the left button is to kill and the right one to cancel.

If after running ps -A we get something including 14550 pts/0 00:00:01 okular and 14559 pts/0 00:00:01 okular then kill -9 14559 will kill only the second okular while killall okular or pkill okular will kill both.


ls by date
The option -t sorts the output by last modified date. It is more convenient to use it in combination with -a" and -l for more complete information.

ls -lat *.txt to list the text files from older to more recent, ls -latr *.txt to do the same in reversing order.


mencoder
It is an alternative to ffmpeg to convert video files that in fact uses it.

mencoder input.mp4 -ovc xvid -xvidencopts fixed_quant=2 -oac mp3lame -sid 0 -subfont-text-scale 3 -o output.avi converts input.mp4 into output.avi. I found this useful to see the result in a PS3.

mencoder -idx input.ogv -ovc lavc -oac mp3lame -o output.avi converts input.ogv into output.avi.

The output files are usually very heavy. To suggest an approximate size it can be used a double pass encoding of the following kind:

rm divx2pass.log
mencoder input.mp4 -ovc xvid -oac mp3lame -xvidencopts pass=1 -o /dev/null
mencoder input.mp4 -ovc xvid -oac mp3lame -xvidencopts pass=2:bitrate=-5000 -o output.avi

Here -5000 is the suggested size in Ks with a minus sign. If the sign is not included then it actually means the bitrate. It is very convenient to put it in a shell script.

There is information here here.
A possibility is using mencoder with the firefox add-on video-downloadhelper.

mogrify
It is very similar to convert. The image is overwritten with format and it allows to massive changes of the format.

mogrify -format eps *.png convert each eps image into a png image.


mv multiple files
The only efficient ways to do it is using shell scripts.

To change all the extensions .JPG to .jpg run the script
#!/bin/bash
for file in *.JPG
do
     mv ${file} ${file%.JPG}.jpg
done

To move filen.txt to new_filen.txt run the script
#!/bin/bash
for i in $(seq 2 13)
do
     mv file$i.txt new_file$i.txt
     echo 'Rename' 'file'$i'.txt' to 'new_file'$i'.txt'
done

To move input.* to output.* run the script
#!/bin/bash
for file in input.*
do
    mv "$file" output"${file#input}"
done


nslookup and dig
The command nslookup followed of an IP number gives the name and vice-versa. With dig the IP number must be preceded by -x

The output of nslookup solfeo.mat.uam.es includes Address: 150.244.21.103 and the output of The output of nslookup 150.244.21.103 includes Name: solfeo.mat.uam.es. In the second case with dig (which seems more verbose) the syntax is dig -x 150.244.21.103.


PDF to lighter PDF
Sometimes after scanning a document the resulting PDF is too heavy. It is possible to reduce the size with gs as in the example

gs -sDEVICE=epswrite -r300 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=outputfile.eps file.eps sets the quality to 300 dpi.

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output_file.pdf input_file.pdf generates output_file.pdf out of input_file.pdf with the lowest quality.

In the last example the possibilities for -dPDFSETTINGS from lower to upper quality are /screen, /ebook, /printer and /prepress.

PDF to PS
In principle this is as easy as printing the PDF in a PS file or using the command pdf2ps but sometimes I have had problems with the result. An alternative is pdftops that has worked to me but sometimes messing the margins. In this case a previous treatment with pdfcrop solved the problem.

pdfcrop doc.pdf -margin 10 adjusts the margin of doc.pdf and pdftops -paper A4 doc-crop.pdf converts it into doc.ps


pdfcrop
As the name suggests crop or adjust the margins of a PDF file. The margins are specified the option --margins.

pdfcrop doc1.pdf doc2.pdf generates doc2.pdf removing the margins of doc1.pdf.

ppdfcrop doc1.pdf doc2.pdf --margin 12 works as before but adjusting the margins to 12pt.

pdfcrop --margins '-10 -15 2 -120' doc1.pdf doc2.pdf specifies, in the order, the left, upper, right and lower margins.


pdftk
This application is a toolkit to manipulate PDF files.

pdftk file1.pdf file2.pdf file3.pdf cat output total.pdf merges file1.pdf, file2.pdf and file3.pdf into total.pdf.

When some of the files have an odd number of pages we may need to append blank pages. Recall that convert xc:none -page A4 blank.pdf creates the blank page file blank.pdf.

pdftk inputfile.pdf cat 1-9 11-end output outfile.pdf omits the page 10.

pdftk inputfile.pdf dump_data output report.txt generates a report with some information about the PDF file.

pdftk inputfile.pdf dump_data | grep NumberOfPages indicates the number of pages of the PDF file. This can be useful in shell scripts.

The following script extracts the first and last page of the PDFs in a folder and put each couple in a PDF in the folder ./result.

#!/bin/bash
# pdfpages

for mfile in *.pdf; do
    pdftk $mfile cat 1-1 end-end output './result/'${mfile%.*}'2_pages.pdf'
done
The documentation and examples are here.
An alternative to merge files is gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=total.pdf file1.pdf file2.pdf file3.pdf
See also this.

pdftotext
It converts a PDF file into a text file. For languages different from English it might be necessary to specify the encoding. To get a list of all possibilities run pdftotext -listenc. The option -layout respects the format of the pages.

pdftotext doc.pdf generates doc.txt.

pdftotext -layout -enc Latin1 doc.pdff generates doc.txt. using the encoding latin1 that seems to be suitable for Spanish and preserving the layout.

pdftotext doc.pdf - | wc -m counts the number of characters in doc.pdf

Expanding the last example, once I used pdftotext in combination with pdftk to decide if a fairly long PDF file, input.pdf, had less than 3000 characters per page and I used the following schell script:

#!/bin/bash
# characters per page

fil=input.pdf

# compute number of pages
npages=$(pdftk $fil dump_data|grep NumberOfPages| awk '{print $2}')

count=0
# for loop pages
for npag in $(seq 1 $npages)
do
    # Extract page npag to tmp.pdf
    pdftk $fil cat $npag-$npag output tmp.pdf
    # Count number of characters of tmp.pdf
    nchar=$(pdftotext tmp.pdf - | wc -m)
    # If greater than 3000 show page number and characters
    if [ $nchar -gt 3000 ]
    then
        ((count++))
        echo $npag '->' $nchar '...' $(($nchar-3000))
    fi
done
echo 'There are' $count 'pages with more than 3000 characters'
echo 'Done'


ps2pdf
It converts PS to PDF. The option -sPAPERSIZE allows to specify the size avoiding to lose the format.

ps2pdf file.pdf generates file.ps

ps2pdf -sPAPERSIZE=a3 file.pdf generates file.ps preserving the A3 size.


psbook
Reorder the pages of a PS file to put them in the right order to make booklets.

Once I found useful bsbook in combination with pstops and psnup to generate a book with 4 pages of size A5 printed in a two-sided A4 with the following commands:
psbook -s4 input.ps output.ps
pstops '2:0L@.69(20.3cm,-2.0cm)+1L@.69(20.3cm,13.05cm)' output.ps > output2.ps
psnup -1 -b1mm output2.ps output.ps
ps2ps output.ps output2.ps


pstops
The man description says that "rearranges pages from a PostScript document" but it has more possibilities.

pstops '0@1.33(-145pt,-15pt)' input.ps output.ps scales the pages with a factor 1.33 and translates the result with (-145pt,-15pt)

pstops '2:0L@.75(21.5cm,0)+1L@.75(21.5cm,12.75cm)' input.ps output.ps each two pages give one with certain margins. Changing 0.75 by other number one controls the page size.

I used pstops '2:0U@1.2(22.0cm,24.0cm)+1U@1.2(22.5cm,37.5cm)' input.ps output.ps to print a beamer presentation and pstops '4:0L@.8(17cm,0)+1L@.8(17cm,11.85cm)+2L@.8(27cm,0)+3L@.8(27cm,11.85cm)' input.ps output.ps for a four page layout.

See also the examples in man pstops. A less powerful alternative way of grouping pages in blocks of n is psnup -n. For instance psnup -4 input.ps output.ps but in my experience it gives some problems with the margins.

recode
See the examples.

recode utf8..iso-8859-15 myfile.txt the file originally encoded with utf8 (the usual codification in Linux systems) is re-encoded as western European.

recode iso-8859-15..utf8 myfile.txt the inverse re-encoding.

See iconv.

script
It stores everything displayed on the terminal. Use exit to finish.

script output.txt stores the session in output.txt.


tee
It writes the standard output in a file.

ls | tee output.txt lists the files and keeps a copy of the output in output.txt.


vi (mini cheatsheet)
There are two modes, the command mode and the input mode. From the latter one changes to the former with ESC and typing : one can use the special commands. The most important commands are:
:q!
quit without saving
:w file_name
save as file_name
:x
save and quit
:e file_name
open file_name
:u
undo the last command

In command mode h, j, k and l behave as the arrows if they do not work. Some commands are:
x
delete character to the right (under) of cursor
dd
delete the current line
u
undo the last command
J
append the following line to the current one
.
repeat the last command (e.g. x.... to delete 5 characters)

To change to input mode the basic commands are:
i
write from the cursor
a
write after the cursor
A
write at the end of the line
C
delete the rest of the line and write
o
append a new line after the current line and write


YaST: global codification UTF-8
In opensuse, to avoid a global codification with UTF-8, in YaST find System>Languages and click on Details in Primary Language Settings then uncheck Use UTF-8 Encoding. It is convenient to restart the system. The preferences of each editor must be adjusted to the chosen default codification.

nom3