» about » content » twitter » github »

Life on the High-Prairie of the GNU/Linux operating system; The Command-Line

Prelude

Before we start, I must tell you that I am not a command-line purist; I use X-Windows on GNU/Linux often (I use graphical browsers, editors, mail-clients etc.) and I even do quite a bit of work on Microsoft Windows (and my Windows work is mainly GUI (Graphical User Interface) oriented, I use the Windows command-line (DOS) command-prompt) too, but it's not yet as powerful and flexible as the GNU/Linux CLI (command-line interface)), although I add some POSIX commands from here (that have been compiled for windows use) to enhance my command-line experience.

So while I spend a good amount of time using a GUI, I do, however, like the raw power of the GNU/Linux command-line (my preferred shell being bash) and do a lot of work in it, sometimes out of necessity (like when connecting to my server at home from work), and sometimes because I like the simplicity of text. Being a writer, text is the paint of my artform. Although I am not an expert command-line user, I think that I've muddled through the usage of it for long enough to write a document that can be useful to myself and a few others. I try to cover a few of the command-line tools and commands that I find the most useful; while not a complete list, I write this in the hope that it will add to the exciting learning experience that is GNU/Linux. Many commands mentioned here (but not all) will work just fine on several other POSIX based operating systems like FreeBSD (and the other BSD's), QNX and many more, including free add-ons for Microsoft Windows like these tools, and the favorite amongst Windows users who want more UNIX-like tools, Cygwin.

This is a document in flux, you are welcome to make suggestions that you'd like me to add, or, as it is released under the GDFL, you can take a copy and modify it as long as you make the changes available for everyone.

Note that commands that can be run from the command-line are usually shown like: this.

Commands/Tools

adduser : adds a new user (must be root)

alias : adds aliases for commands alias foo=ls

apropos : gives an idea what command to use apropos image

at : schedule a one-time job at 8am 3/31/02 followed by echo "test time!" | mail -s "reminder" roqetman and then ctrl-d, this will schedule an email "reminder".

atq : list all the jobs scheduled by the at command

atrm : remove a job scheduled with "at"

banner : creates a simple banner (must be under 10 chars) banner "Hello!"

batch : bundle a set of commands into a batch file for later execution batch -f batchfile (contains the commands) more info: here

bc : a simple calculator (ctrl-d (or 'quit') to exit)

calendar : a calendar application for storing appointments and reminders. http://librenix.com/?inode=4253, there is also there is a very cool program that does similar things called pal (see pal entry), or if your distribution of GNU/Linux doesn't have the bsdutils package available (like Red Hat/Fedora): http://bsdcalendar.sourceforge.net/. Here is another python+ncurses based calendar program; note that I haven't tried this one out yet as I'm quite happy with the other calendar, but I'll give it a try sometime.

cat : sequential file reader cat file.txt|sort -u|wc -1 cat -s file.txt (removes linebreaks) cat -n file.txt (adds line numbers)

cd ~/ : change to home directory

cdrdao : writes audio CD-Rs in disc-at-once mode (good for copying cd's) - see readcd for data CD's

cdrecord : burn cds cdrecord -v -dao dev=ATAPI:0,0,0 /path-to-filename/filename.iso (for non-scsi), or cdrecord dev=0,0,0 -dao -data KNOPPIX_V3.1-10-09-2002b-EN.iso (see mkisofs to create a .iso file, and md5sum on validating a .iso file)

chgrp : modify group permissions chgrp newuser filename

chmod : modify permissions chmod 644 filename.html (see "changing file permissions" in the tips'n'tricks section of this document for more help & examples)

chown : change owner chown newuser filename

comm : compare files for similarities (opposite of diff) comm -1 -2 file1 file2

crontab : tool to add scheduled tasks (cronjobs) to cron crontab -e, to use an editor other that vi, add this to your .profile file: export VISUAL=pico (here I've used pico as an example alternate editor)

cu : serial port client (functinal equivalent of HyperTerminal on Windows)

date : view date, or date -s 16:45 (sets the time to 4:45pm) date -s 'Sun Apr 4 07:36 EDT 2004', date +%x (show date in a format, see man date for more formats)

df -h : disk space left (shown in megs)

diction : grammar checking tool, see also style, created from the original UNIX "Writer's Workbench"; available here.

diff : compare files for differences diff file1 file2

dig : find ip information dig http://www.yahoo.com

dmesg : show kernel ring buffer (the messages that appear on boot)

du : shows directory usage du -h (makes the list human-readable) du -s (gives dir total size) du -x | sort -nr | head -15 (gives the largest 15 dirs). See ncdu for a handy utility that's easier to use.

echo : output something. echo $PATH (displays the contents of the $PATH variable), echo Hello World (displays "Hello World).

enscript : converts text files to PostScript, HTML, RTF, ANSI, and overstrikes. Can also send output to printer; enscript -p testfile.ps testfile.txt (converts text file to postscript file), enscript testfile.txt (converts file to postscript and sends to printer), enscript -f "Times-Roman10" testfile.txt (converts file to postscript with a font of Times New Roman size 10 and sends to printer).

env : display environment variable settings.

ethtool : display or change thernet card settings ethtool eth0. Also see mii-tool

export : configuration settings export TZ=EST (change time zone to Eastern Standard Time) or export TZ=EST5DST for daylight savings time

expand : convert tabs to spaces; expand filename. Also see unexpand

find : search find . -name "*.pdf" (finds all files with a .pdf extension) find /usr/doc/Linux_HOWTO/* | xargs head -v -n 20 | less (searches through all the HOWTO's giving the header of each) find . -name '*' | xargs rm (removes everything in a directory (gets pas the limitations of rm))

fmt : format a file fmt -u filename (removes spaced lines) fmt -s filename (retains only 1 blank line between each), fmt -w 80 filename (formats file with linebreaks after 80 characters)

fortune : give a quote (fortune cookie) /usr/games/fortune

ftp : ftp (a nicer one to use is ncftp (kind of browser-like))

gcc : The GNU Compiler Collection, a full-featured ANSI C compiler, supports K&R C, as well as C++, Objective C, Java, and Fortran. gcc *.c -o appname (compile C source)

grep : search for strings within output/files grep -irH "qahra4" *.PRG (recursively looks thru all *.PRG files for case insensitive match of "qahra4" and prints filename) cat gplbrownies.txt | grep "Linux Rocks" or grep -i "string" search_file >new_file (the -i = case insensitive)

gunzip : file decompression utility gunzip filename

gzip : file compression utility (also see tar -z) gzip filename

help : strangely enough, gives some help, also see apropos

ifconfig : used to configure a network interface, can also just give information (like the ip address) /sbin/ifconfig eth0 (handy for DHCP info)

info : info reader (similar to man, documentation in the GNU/Linux world is spread about unfortunately) info emacs

init : process control initialization (almost like rebooting), if you are on runlevel 3, and you've modified you /etc/inittab to runlevel 4 (for graphical login), then you can type init 4, alternatively, if you've modified you /etc/inittab to runlevel 4 (for command-line login), then you can type init 3.

installpkg : Slackware package installer; installpkg filename.tgz, if you're concerned about dependencies, look at swaret. Also see removepkg, upgradepkg and pkgtool.

ispell : spell checking; ispell filename.txt, echo foo |ispell -a checks the spelling of a single word (in this case "foo").

kernelversion : gives what it says

kill : kills a process (see ps) kill -1 `cat /var/run/httpd.pid` (kills apache)

last : show users who last logged in

ldd : show dependencies, ldd /usr/bin/gaim (requires the full path, which you can get with which as in which gaim

less : show a file in a scrollable form less filename cat filename | less

ln : symbolic link ln -s fromexistingpath name (hard link (for directories)) ln /path/filename newfilename (soft link)

locate : find a file (this is tyically depreciated, and a link to slocate is actually what happens; (see slocate)

ls : directory listing, ls -lpa (listing with all the trimmings), ls -l `which vi` gives you the location including symlinks.

lspci : utility for displaying information on PCI buses in the system and all devices connected to them; lspci -v give more info, lspci -vv gives all info it can.

lynx : text web browser; lynx http://www.slashdot.org

make : build tool, mainly used to build C programs, but can be used to selectively run anything, the most common version is this one. You can also use ant. Which one to choose?

man : show manual (help) page man sendmail man catdoc | col -b > catdoc.mantxt (outputs man page into plain ascii)

md5sum : validates a downloaded .iso file (usually found in the same download place as the .iso itself) md5sum yarrow-i386-disc1.iso

mii-tool : view interface status (like network speed): mii-tool

mkisofs : make an .iso image. mkisofs -l -o imagefilename.iso /path/to/tree (see cdrecord if you want to burn this image to cd)

mount : mount a device (the mount-point must exist; usually a directory created off the /mnt directory), mount /floppy mount /dev/fd0 ~/tmp mount -o loop /home/roqetman/downloads/yarrow-i386-disc1.iso /mnt/point (mounts an .iso file), mount -t vfat /dev/sda1 /mnt/USB (mounts a USB camera), mount -t vfat /dev/sda2 /mnt/iPod (mounts an iPod (haven't tested this one yet, the iPod has to be formatted for Windows)).

naim : an aol chat client, available here.

ncftp : a command-line ftp-browser (nice'n'easy to use)

ncdu : A very handy disk usage analyzer with an ncurses interface, available here. To use, just cd somewhere and type ncdu and give it a few minutes to scan the directories, then you can navigate with your arrow keys or type ? for help.

ndc restart : restarts named server

netconfig : sets up network configuration

netstat : shows network connections, routing tables, interface statistics, masquerade connections, port information and netlink messages netstat -lntpe, netstat -na, netstat -at (look for www for apache)

newaliases : updates the /etc/mail/aliases.db file

nmap : Network exploration tool and security scanner; nmap -v somewhere.com

pal : a very cool calendar and appointment reminder program, available here.

pkgtool : Slackware interactive package tool (installs, uninstalls and gets info on packages); also see installpkg, upgradepkg and removepkg.

pr : convert text files for printing pr -4 -t filename (place text into 4 columns), pr -d -t filename1 > filename2 (double spaces text).

printenv : show environment variables

ps : show processes currently running and the users who own them ps auw or ps auxw (shows all running jobs)

pwd : where am i?

readcd : read or write data CD's - see cdrdao for audio CD's

removepkg : removes Slackware package; also see installpkg, upgradepkg and pkgtool

route : show / manipulate the IP routing table

rm : delete rm -f file.txt(-f doesn't prompt for each file) rm -R directory(to remove entire directories and their sub-directories)

rmdir : remove empty directory (use rm -r directory for full directories with sub-directories)

rpm : RedHat Package Manager rpm -Uvh package.rpm (install) rmp -q package (query which version is installed) rpm -e package.version (uninstall)

rpm2tgz : converts a RedHat RPM to a Slackware TGZ package rpm2tgz filename.rpm (then you can do installpkg filename.tgz)

rsync : syncronize files; rsync -e 'ssh -p 3051' -rva /cygdrive/c/roqetman/test roqetman@myhost.org:/home/roqetman (this keeps the remote in sync with the local, the ssh... part is only necessary if your sshd port is different from the default). Add --delete to remove files during transfer.

sc : a simple spreadsheet

screen : a powerful full-screen window manager (allows you to run multiple applications simultaneously from the command-line and easily switch between them; you can even copy and paste between screens) - and most importantly, a screen session will continue in the background if your ssh session gets disconnected. Use ctrl-a ? from inside screen for help, ctrl-a c for a new screen within screen, ctrl-a SPACE to switch to the next screen, if you got disconnected, you can reattach to your last screen session by typing screen -r.

script : records a log of the session script filename

sed : find/extract strings\etc. sed -n 's/^.*\(myString\).*$/\1/p' myFile > NewFile, for i in *cfg-sample; do mv $i `echo $i | sed -e s/cfg-sample/cfg/`; done; (rename a whole bunch of files)

set : read and write system/environment variables

sftp : secure ftp sftp roqetman@212.23.77.19 (this can be the domain name too)

shutdown : halts/reboots system shutdown -h now (halts machine) shutdown -r now (reboots machine) shutdown -h 5 "lazarus going down for a while due to thunderstorms" (preferred shutdown) Note on RH, use /sbin/shutdown

slocate : find a file (filenames are periodically (and automatically) stored in the slocate database by the updatedb utility (see updatedb); locate filename (find filename), locate -i filename (find filename case-insensitive search)

slrn : an easy to use NNTP / spool based newsreader, available here.

sort : guess!

ssh : secure shell ssh -l roqetman 212.23.77.19 (this can be the domain name too), ssh user@myhost.com sudo /etc/init.d/apache2 restart (logs in and runs a command)

style : grammar checking tool, see also diction, created from the original UNIX "Writer's Workbench"; available here.

su : login as another user su (logs in as root(super-user)) su roqetman (logs in as roqetman)

swaret : a dependency-checking package manager for Slackware: http://www.swaret.sourceforge.net; swaret --faq (faq) swaret --manual (manual) swaret --update (updates itself) swaret --update -a (updates everything, controlled by /etc/swaret.conf file, swaret --search kde (searches for kde packages to install), swaret --install xxx (install package xxx)

tail : display the tail-end of a file tail -n50 /var/log/messages (-nxx optional)

tar : archive utility tar -zxvf archivename (extracts from an compressed archive) tar -cvf archivename filename (creates archive and inserts file) tar -Avf archivename filename or tar -czvf archivename.tar.gz *.txt (compresses and adds into an archive) tar -czvf archivename . (creates and inserts files and subdirectories to a compressed archive (including hidden files)) tar -tvf archive.tar (views the files in the archive)

top : display Linux tasks; top

tput : change terminal colors tput setf 4 # tput setf {fg color number}, tput setb 2 # tput setb {bg color number}

uname : system information uname -a - also look in /proc - just cat them all all you want to know down to the Social Security Number of the guy that installed the Hard Drive is in these files. to get kernel version uname -r

unexpand : convert spaces to tabs, unexpand filename. Also see expand

unzip : uncompress files (compatible with winzip) unzip -r filename.zip

updatedb : update the slocate database (see slocate).

userdel : deletes a user (zaps info from /etc/passwd & /etc/group) to zap his directory too use "-r" userdel -r username

w : show's who's logged on (gives job info too)

watch : execute a program periodically, showing output fullscreen

wc : word count

weather : checks the weather typically by connecting to rainmaker.wunderground.com, to use follow the command with a 3-character code for the city; weather NYC.

wget : download a file wget ftp://ftp.aarnet.edu.au/ls-lR.gz can be used in combination with "at". To get a whole site (upward hierarchy): wget --recursive -L http://www.users.drak.net/caudata/ wget -b ... (puts job in background - good for getting .iso files), wget -pk http://www.users.drak.net/caudata/ (gets site and converts all links to local links (good for local viewing)), wget -rpk --level=0 http://www.joebarr.org (mirrors a site). wget -rEKk http://www.pjprimer.com (another way to mirror a site).

whatis : searches whatis database for simple information whatis fetchmail

who : show's who's logged on (can also use w)

which : show location path of executable which perl

zgv : view images from the console, available here, zgv supports most popular formats, and allows panning and fit-to-screen methods of viewing, slideshows, scaling, gamma adjustment, etc.

zip : compress files (compatible with winzip) zip -r filename.zip filename

What can I use for...

(see command-lists above, or sections below for more on some of these tools)

checking email : see mail section below.

editing text : see editors section below.

browsing the internet : lynx; already installed on most distributions, but if you need it, it can be found here

accessing NNTP newsgroups : slrn

instant messenging : naim, many more here

appointments, reminders : calendar, at, pal.

grammar checking : diction and style.

spell checking : ispell or aspell.

word-processing : To be honest, I don't use a console-based word-processor; There are some out there, but I haven't found one that is still supported. If anyone knows of a free console-based word-processor, please let me know so I can try it out and possibly add it to this document. I simply use an editor; vim is my preferred tool for writing. I wrote a script in Perl that converts standard ascii text to Rich Text Format (RTF) which is readable by most word-processing applications. I then check my final draft in OpenOffice or my latest favorite AbiWord (yes, I know GUI applications) before I send it in for publication. There are some who prefer a markup solution; this is probably a good idea for precision typesetting, but has a steep learning curve.

creating a PDF document : Perl has various libraries that allow you to create pdf documents from text. Also look at antiword.

read an MS-Word document : Try antiword, or catdoc (reads an MS-Word document and output its content as plain text).

viewing images : see zgv entry above.

revision control : see section below.

installing applications : (especially those not in your distibutions package format) - checkinstall. It can create packages and then you can use your package manager to remove it at a later time if desired.

uninstalling applications : although most packages you untar and install give you ample *install* information, they give scant *uninstall* information; a common method of uninstall is: make uninstall (in the directory you originally did the "make install" from). RPM is a little more user friendly in this regard (see details on RPM above).

reading MS Word documents : this is easy using GUI tools (like OpenOffice and AbiWord), but if you want a simple way to read them from the command-line (by converting them into text), install a package called "catdoc", or use antiword (haven't tried this one yet).

page-up/down in console : to page-up in a console, use shift-pgup, down use shift-pgdown

allowing users to mount drives : /dev/fd0 /floppy auto defaults,noauto,user 0 0 /dev/cdrom /cdrom auto defaults,noauto,user 0 0

removing carriage returns (a la Windows) : perl -pi -e 's/\r\n?/\n/g' filename

browsing SMB/Samba shares in Linux : There is a handy tool called smbc (Text mode SMB network commander)

forwarding email to another email address when it arrives : create a file called .forward in your user directory and place the other email address in the file. For more than one email address, delimit with a comma.

running something as root : sudo; use to setup a command that only root could normally run, use visudo; here is an example to allow the use of the VPN service: Cmnd_Alias VPN=/etc/init.d/vpnclient_init <newline> roqetman ALL=NOPASSWD: VPN Now, the command can be run from user (roqetman) as: sudo /etc/init.d/vpnclient_init start

changing file permissions : 3 sets of 3, order - Owner, Group member, world

eg: 4 -rw-rw-r-- 1 warthawg warthawg 1163 Jan 23 09:25 story.txt chmod 660 story.txt

 chmod..Read..Write..Execute
 value 	
 7......Y.....Y......Y
 6......Y.....Y......N
 5......Y.....N......Y
 4......Y.....N......N
 3......N.....Y......Y
 2......N.....Y......N
 1......N.....N......Y
 0......N.....N......N
    

burning a cd (example, obviously a blank cd would have to be present) : 1st: mkisofs -o imagefile.iso /backup/20040206103030 then cdrecord dev=0,0,0 -dao -data imagefile.iso, also see the commands readcd and cdrdro above.

system information : look at the contents of the files in the /proc directory.

output redirection : to redirect output from a command into a file, use ">" ls > dirlist.txt, to append rather than overwrite use ">>" ls >> dirlist.txt. Also goes the other direction grep -i spindle < haystack.txt >>search.results.

error redirection : grep -J spindle < nofile.txt >>search.results 2>error.log, if you want to combine errors with output, try this: grep -J spindle < haystack.txt >>search.results 2>&1.

redirection of errors back to standard output : find ~ -type f 2>&1 | less (This will let you use "less" to view a list of every file in your home directory)

accessing an Apple iPod : mount -t vfat /dev/sda2 /mnt/ipod (after the directory /mnt/ipod has been created), and then use GNUPod, a collection of Perl scripts that can transfer music files to your iPod, lastly unmount it; umount /mnt/ipod, and then eject it (to stop that "Do Not Disconnect" message) eject /dev/sda2. For a simple list of files on your iPod, use this command: find /mnt/ipod/iPod_Control/Music -print, to simply find a song on your iPod use: find /mnt/ipod/iPod_Control/Music -print | grep songname.

download podcasts : bashpodder

viewing running tasks/programs : top

viewing graphic files (and powerpoint) : try xnview, it can view many, many formats

killing/stopping a task/program : either type top followed by k and then the PID number to kill, or type ps auxw and then kill PID (where PID is the number of the task you wish to kill)

running multiple programs from a single window : see screen above.

entertainment/games : try nethack, a single-player multi-level dungeon rogue-style exploration game, and take a look into Interactive Fiction, or if you like to interact with others try a MUD (Multi User Dungeon) - you can connect to most of these using telnet. You can also find GNU Chess on most linux systems; type gnuchess.

managing files : Midnight Commander is the most commonly used; mc.

programming any manner of cool things : perl, take a look here.

printing files : here is a good place to start, it gives command-line examples, you need to have setup CUPS to print however, look here for examples on how to do that. Here are a few commands: lpr filename (print to your default printer), lpr -P printername filename (print to a specific printer), lpstat -d -p (see what printers are setup, you can use the web page (see above), or use the command "lpstat -d -p". The -d tells you the default printer, and the -p lists the printers available, and their status).

downloading bittorrents : ctorrent is quite good for this. Once installed, type: ctorrent -e 0 the.file.iam.going.to.download.torrent

playing music : try mp3blaster, moc, cmus and many more.

encrypting/decrypting a file : use openssl (usually installed by default), to encrypt (using 256-bit AES in CBC mode): openssl enc -aes-256-cbc -salt -in file.txt -out file.out to decrypt: openssl enc -d -aes-256-cbc -in file.out (enc : Encoding with Ciphers).

issue/bug tracking : Take a look at Task Juggler.

Editors

There are as many as stars in the sky (well, OK, not *that* many, but there are a lot); here are my favorites...

vim : vi improved (gvim = graphical (x-windows) vim) - http://www.vim.org, I have a document on this here

emacs : very extensible editor, for console use "-nw" emacs -nw FILENAME - http://www.gnu.org/software/emacs/, I have a document on this here

pico : simple editor that comes with pine - http://www.washington.edu/pine/, I have a document on pine et al here

Mail

mail : primary mail program (to send) echo -e "message" | mail -s "subject" emailaddress or simply mail username (. to end message), (~rfilename to insert a file(eg ~r.sig)), mail -f (to read old mail (once read, mail is saved in the file ~/mbox)), mail -f /var/mail/othermailbox (to check other mailbox that you have permissions to - handy if you have changed to another user using su).

mailx : a newer version of mail, in some of the more up-to-date distributions, it even has a handy option to add attachments like so: echo "I've attached a file to this email" | mailx -s "Here's a file" -a AFile.txt anemailaddress@somewhereorother.org

metamail : application for displaying attachments (used with "mail" (above)from inside mail after the & prompt, type "x" and then "metamail filename")

metasend : application to send attachments metasend (it prompts for the rest)

mailto : same as mail, but it handles the sending of attachments (use "~*" on a line to insert an attachment)

fetchmail : collects mail from one or more POP3 accounts and stores it in your local account the .fetchmailrc file must have the account details within it .fetchmailrc:poll pop.provider.net proto pop3 user "jsmith", with password secret1, is "jsmith" here;, also fetchmail -k ("keep" don't delete from server). I have a document on this here.

frm (or nfrm) : list from and subject of selected messages in mailbox or folder

mailq : show mail queue (to delete from queue look in /var/spool/mqueue)

alpine : a fully functional easy to use mail program (Note # does a "Compose" using Roles) - http://www.washington.edu/alpine/ alpine -p (For Different Pinerc File), alpine -i (to open folders), I have a document on this here

mutt : another fully functional mail program mutt -s "This is a subject" name@place.domain < textfile (to send direct from command line) - http://www.mutt.org

sendmail : basic sendmail prog. use pine or mutt, but if you really want to send mail using this: /usr/lib/sendmail name@place.domain <textfile (Note to restart sendmail if you make any changes to it's configuration, use: killall -HUP sendmail) sendmail -q (flush mail queue) To find out sendmail's version #: echo \$Z | /usr/sbin/sendmail -bt -d0

msmtp : a utility that sends email to an external SMTP server (very handy if you want to use mail, mutt or any other MUA, but don't want to setup sendmail or other mail-server on your machine. You can find it here

Revision/Version Control

There is the old tried and true hrcs (Revision Control System), but the one I currently use is git.

Other Concepts

file completion : this is part of the bash and zsh shells (and several others), you use the TAB key to complete a word, for example if you are trying to change into the docs directory, you could type ch do<TAB> and the word do would change to docs. Or try echo $PA<TAB> should change to echo $PATH.

Mac OSX Command-Line

Wile most of the above will also work on the Mac OSX command-line, there are a few extras. This page coveres many of them nicely: The Terminal

© Roqet 2018 :: 2018-05-11 17:07:10