User Tools

Site Tools


how_to_use_the_command_line_interface_-_examples

This is an old revision of the document!


How to Use the Command Line Interface - examples

By Peter Tesar

Here are examples that demonstrate a broad range of useful tasks that can be performed from the command line. Some tasks show more than one command and some commands have been repeated to show a few of its useful options. For more comprehensive information about a particular command, use the man pages or search the internet.

It is assumed that the user has a basic understanding of Linux and the command line. For more details see the article "How to Use the Command Line Interface".

Note: each task is preceded with the pattern ‘T>’.

Here are some points to be aware of.

Linux is case sensitive.

Commands have the form (syntax):
$ command [-option] argument

Some commands or packages may need to be installed.

Assume that the hard drive is labeled /dev/sda and a USB pen drive is /dev/sdb1.
The last number is the partition number.

All of these examples work on the Ubuntu 12.0.2 version of Linux using the BASH shell and the virtual console. Some devices may not be recognized.
E.g. scanners or printers.

If the Desktop was used to establish a network connection, or install the Codecs, they will be in effect in the console. The codecs contains third party software that is necessary to access non open source formats.
E.G. the .mp3 audio compression format is not open source.

T> Show information on AC adaptor, battery status and other devices:
$ acpi -a - AC adapter
$ acpi -b - battery capacity
$ acpi -V (cap) - every device

T> Show date and time, set date, time and time zone:
$ date - show system date and time
$ date -u - show universal time (GMT)
$ date --date='14 days ago' - show date 14 days ago
$ date --date='next month' - show the date next month
$ sudo date -set='30 may 2013 18:00' - set system date and time
$ sudo date -s '05/30/2013 18:00' - set date and time
$ sudo date +%y%m%d -s '20130530' - set the date only
$ sudo date +%t -s '18:00' - set time only
$ sudo date +%t%p -s '06:10am' - set time, morning
$ ntpdate -q - query NTP server date/time, don't reset your system
$ sudo ntpdate pool.ntp.org - sync the Linux time with the NTP Pool time Server
$ sudo dpkg-reconfigure tzdata - set time zone from geographic region and cities lists

T> internet connection check, send packs to a host:
$ ping -c3 -a google.com - 3 pack check with audio verification

T> List hardware and PCI devices, internal cards and USB headsets:
$ lsusb - list USB devices including printers and scanners
$ sudo lshw - hardware
$ lspci -short - ethernet and wireless
$ lspci -nn - show vendors, codes as both numbers & names
$ sudo ceni - ethernet & wireless hardware, ctrl+c to cancel
$ aplay -l - USB headsets
$ wodim --devices - look for optical CD/DVD devices

T> The account you are logged in as, change your password and Linux kernel version:
$ who am i - three words
$ passwd
$ uname -ar

T> Shutdown or reboot:
$ sudo init 0 - shutdown/halt
$ sudo init 6 - reboot
$ sudo halt
$ sudo reboot
$ sudo shutdown -h now
$ sudo shutdown -r now

T> Send a text file to the default printer:
$ lp report.txt
Print with default option values for characters&lines per inch:
$ lp -o cpi=10 -o lpi=6 report.txt

T> Archive, compress (zip) and extract (unzip):
$ tar -cf file-all.tar file1 file2 file3 - archive without compression
$ bzip2 -k file-all.tar - compress to a .bz2 file, keep input
$ tar -cjf file-all.tar.bz2 file1 file2 file3 - 'j' option combines the above 2 examples
$ tar -xvf application.tar - options: extract, verbose &file file-name
$ tar -zxvf application.tar.gz - extract using g zip

T> Backup and restore the boot loader and partition table:
$ sudo dd if=/dev/sda of=bkup.bin bs=512 count=1 - backup the MBR & partition table
$ sudo dd if=bkup.bin of=/dev/sda bs=446 count=1 - restore only the Mbr
$ sudo dd if=/dev/zero of=/dev/sda bs=512 count=1 - write zeros to the first 512 bytes
dd options: in-file, out-file, block size, block count.
Note: you can use 1K, 1M or 1G (cap).

T> Combine many files into one or copy from the keyboard:
$ cat file1 file2 file3 > all-files
$ cat > file.txt - capture from the keyboard
Type your data and close the file with ENTER and ctrl+d.

T> Copy a source file to a destination with a few options:
syntax: cp [-options] source destination
-b - backup, rename destination file with '~' appended to the name before replacing
-i - interactive, prompt before overwrite
-r - recursive
-u - update/overwrite only with a newer file
-v - verbose, explain what is being done
$ cp -biu phone.txt Back-dir - backup your phone file if modified
$ cp -r Documents Documents-back - copy documents directory (and its entries) to a new dir

T> Move a file or rename it by moving it to the same directory:
$ mv -i phone.txt Documents - move a file to the Documents directory
$ mv -i phone.txt phone.old - rename a file (in the same directory)

T> Make a directory and verify:
$ mkdir -v dir-name

T> Remove/delete files or directories:
$ rm -iv files* - remove many files and prompt for each
$ rm -I (cap) files* - prompt once before removing more than 3 files
$ rm -fv files* - force removal
$ rm -vr dir-name - remove a full directory recursively
$ rmdir -v dir-name - remove an empty directory & verify

T> Shred/overwrite a file repeatedly to hide its contents
syntax: shred [options] file(s)
--iterations=n - the default is 3
-u or --remove - delete after overwriting
-v - verbose announcement progress
-z - overwrite with zeros on the last pass (default is random)
$ shred -vz phone.txt
Caution: shred does not work on all file systems.

T> Write zeros to unused partition space:
$ sudo df -h /dev/sda3 - get available space on partition 3
$ sudo dd if=/dev/zero of=file bs=1G count=5 - create a zeros file in available space
$ rm file - delete the temporary file

T> Reset a file's date/time stamp or create a new empty file:
$ touch file-name

T> Download an ISO file. First time, entire file, then only changed blocks:
$ zsync URL
$ zsync http://cdimage.vinuxproject.org/daily-live/current/vinux-i386.iso.zsync

T> Burn a live CD/DVD or create a live USB pen drive (using an ISO file):
$ wodim blank=fast - erase a read/write disk
$ wodim speed=0 linux.iso - use the slowest speed
$ sudo dd if=vinux.iso of=/dev/sdb bs=2M (cap)

T> Convert text to speech (TTS) using the espeak voice synthesizer:
$ spd-say hello - test the speech dispatcher
$ espeak -s300 -ven -w file.wav -f file.txt - with high speed & English voice (default)

T> OCR steps: scan, enhance and convert to text.
Scan into an image file:
$ sane-find-scanner -q - look for attached scanners (in quiet mode)
$ scanimage -L (cap) - get attached scanner device name
$ scanimage --device 'avision:libusb:002:002' > image.pnm
$ scanimage -d 'avision' --resolution 200 --mode 'Gray' > image.pnm - more options
$ scanimage --brightness -20 > image.pnm - brightness range from -100 to 100
$ scanimage --format=tiff --mode 'Color' > image.tiff - tiff with double 'f'
$ scanimage -x 100 -y 150 > image.pnm - 4x6 inch colour photo

Enhance the scanned image before OCR (optional):
$ unpaper -T -v image.pnm unpapered.pnm - test with verbose option
$ unpaper image.pnm unpapered.pnm - auto-straighten, align horizontally
$ unpaper --pre-rotate -90|90 image.pnm unpapered.pnm - counterclockwise|clockwise

OCR Engines to convert to text:
$ tesseract image.pnm out-file -l eng - extension '.txt' will be appended to out-file
$ cuneiform -l - list supported languages
$ cuneiform image-unpapered.pnm - defaults: English & output to 'cuneiform-out.txt'
$ cuneiform -l eng -o file.txt image-unpapered.pnm - override default language & output

T> Convert image file formats:
$ convert image.pnm image.tif - tif with a single 'f'
$ convert image.pnm image.jpg
$ convert image.pnm image.pdf

T> Listen to a URL site or play music files:
$ mplayer dir/*.mp3 - q/ESC=quit, space=pause/play
$ mplayer playlist | URL
$ vlc dir/*.mp3 - ctrl+c to cancel
$ vlc URL - ctrl+c to cancel
$ vlc http://www.bbc.co.uk/radio/listen/live/r4lw.pls - BBC radio 4
$ rhythmbox --playlists-file=file

T> Rip music CD's using: abcde, jack (the ripper) & cdparanoia
1) Rip music CD tracks (into ~/abcde…):
syntax: abcde [-options] [tracks]
$ abcde -N -a read,encode,tag -o mp3 -k -x 1-3
Some abcde options:
-N (cap) - non-interactive (batch) mode
-a <ACTIONS> - comma separated
-o output-format - ogg is the default
-k - keep the wav files after encoding
-x - eject disk when finished
<track1 track2 …> or <track-range> - grab individual tracks or a range

2) Rip music CD tracks using jack (the ripper):
syntax: jack [-options]
$ jack - (default) put ogg & log-progress files in jack-label
$ jack -q -k - query tag info from freedb, keep wav files
$ jack -Q --remove-files - all in 1: query, rip, encode, cleanup
$ jack -t 2,5,7-9 - rip tracks 2, 5 and 7 through 9
$ jack --quality 10 - range: -1=low, 6=default & 10=high
$ jack --bitrate 128
$ jack -E lame - use lame to encode to MP3 format
notes on using jack.
'-Q' (cap) or '-q' (lower) to query at start or end of process.
'q' to quit during the ripping process.

3) rip music CD tracks using cdparanoia (from scratched disks),
with data verification & scratch reconstruction capability.
syntax: cdparanoia [-options] span [outfile]
$ cdparanoia -A (cap) - analyze drive caching,timeing and reading behavior
$ cdparanoia -B (cap) - batch, rip entire disk, each track in separate wav file
$ cdparanoia -B artist.wav - output files: track0x.artist.wav
$ cdparanoia -Q (cap) - drive autosense, query, table of contents & quit
$ cdparanoia -q or -v - quiet or very verbose
$ cdparanoia -X (cap) - abort on skip because of imperfect data, delete partial file
$ cdparanoia -z - retry forever if needed, override default 20 retries
$ cdparanoia -Z (cap) - fast, disable all data verification & correction features
$ cdparanoia 2-5 - rip tracks 2 to 5, into 1 output file: cdda.wav
$ cdparanoia -B 2-5 - rip tracks 2 to 5 in batch (separate files)
$ cdparanoia -B -- -3 - extract from beginning of disk to end of track 2
$ cdparanoia -B 10- - rip track 10 to end of disk
$ cdparanoia '1[1:0]' - rip from track 1, offset 1 minute to the end of track
$ cdparanoia "2[:30.12]-2[1:10]" - rip from track 2, time: 30.12 sec to 1 min 10 sec
Note: the '--' double dash distinguishes the span '-3' from an option flag.

T> Rip a video .vob file from a movie DVD:
$ vobcopy -I (cap) - info: list .vob files & the number of chapters in each
$ vobcopy - no options, grab .vob file with the most chapters

T> Convert text files:
$ abiword --to=pdf -o out-file.pdf in-file.doc - also out=txt/rtf/html
$ abiword --to=pdf in-file.doc - same as above, omit the outfile option
$ abiword --to=txt my-file.doc - out file will be 'my-file.txt'
$ antiword mydoc.doc > mydoc.txt
$ dd if=in-file.txt of=out-file.txt conv=lcase - convert upper case to lower case
$ tr '[:upper:]' '[:lower:]' < input.txt > output.txt - translate to lower case

T> Convert/encode Audio/Music Formats:
syntax: lame [-options] in-file.wav out-file.mp3
$ lame -h in.wav out.mp3 - highest quality, j-stereo, 128kbps (cbr default)
$ lame -b 112 in.wav out.mp3 - fixed bit rate 112kbps, j-stereo
$ lame -V 4 in.wav out.mp3 - vbr range: 0=highest quality, 4=default, 9
$ lame -m m in.wav out.mp3 - mode can be: m=mono, s=simple-stereo, d=dual
$ lame -a in.mp3 out.mp3 - mix the stereo input to mono& encode as mono
$ lame --quiet in.mp3 out.wav - output wave quality limited to mp3 compression
syntax: oggenc in-file.wav [-options] [out-file]
$ oggenc file.wav - output to file.ogg
$ oggenc in-file.wav -o out-file.ogg - specify the output file name
$ oggenc file.wav -b 256 - a high-quality encoding, average kbps (still VBR)
$ oggenc file.wav -q 10 - quality rather than bitrate, range: -1/10 default=3
$ oggenc file.wav --managed -b 196 - turn off the normal VBR
$ oggenc *.wav --quiet - use wildcard for the file name

T> Extract or Convert video, extract audio track, get audio duration:
$ ffmpeg -formats - list video formats
$ ffmpeg -i in.vob -ss 30 -t 00:08:00 -y out.vob - extract, start from 30 seconds, time 8 min
$ ffmpeg -i in-video.vob out-video.wma - convert video .vob to .wma
$ ffmpeg -i video.vob audio.wav - extract the audio track
$ ecalength audio.wav - total time in seconds & minutes:seconds

T> Dictionary & Thesaurus definitions, from internet databases:
$ dict word
$ aiksaurus word
note: download databases for offline references:
$ sudo apt-get install dict dictd dict-foldoc dict-gcide dict-moby-thesaurus

T> Interactive spell-check of a text file:
$ ispell report.txt - q=quit

T> Get a web page and save it as file-name (default is index.html):
$ wget URL -o file-name.html

T> Built-in calculator (in interactive and quiet mode):
$ bc -q - 'quit' to quit
$ bc -q -l - load a math library with scale=20
Some bc commands:
+ (plus), - (minus), * (multiply), / (division)
caret - exponent
sqrt(2) - square root of 2
scale=5 - set 5 decimal precision

T> Calendar (displayed in a table layout):
$ cal - display the current month
$ cal -y - display the 12 months of the current year
$ cal 4 2011 - display only April 2011
$ ncal -e 2012 - show Easter, default is the current year

!!End of Article.

how_to_use_the_command_line_interface_-_examples.1377382056.txt.gz · Last modified: 2019/02/09 10:31 (external edit)