HomePage > Components and HowTos > CommandLine

Bash Quick Tips

Here is a list of oneline tips for Bash commands linked to phrases.

The X File (Written by Martin Streicher)
To run X clients securely, use ssh -Y host. DISPLAY is automatically set to encrypt and tunnel X protocol back to the X server.

Rename With Impunity
Do you need to change the extension of many files at a time? Nothing is faster than zsh -c "autoload zmv; noglob zmv -W *.a *.b".

Jump Back (Written by Aram Yegenian)
To go back to the last directory you visited, simply type cd -. (Thanks to Aram Yegenian)

Contract Killer
Want to kill a process the quick and easy way? Try killall /path/to/file, which kills all processes executing /path/to/file.

Shred a File
To delete a file securely, first overwriting its contents (25 times), run shred -u -z -v file.

Expire Passwords
Improve system security by expiring user passwords from time to time. The command passwd -x 30 joe forces joe to change his password after thirty days.

Directories Made Easy
Use mkdir -p to create a hierarchy in one fell swoop. For example, mkdir -p a/b/c/d makes directory d in directory c in directory b in directory a.

To see the list of groups you're a member of, use id -Gn. id has effectively replaced groups and whoami.

What Timezone Is It?
If you want to know the proper name of your current time zone, run the interactive command tzselect. Follow the prompts and use the output in your shell login file.

Faster, Simpler Version Control
Tired of CVS? Try darcs. Creating a repository with darcs is as easy as cd ~/myproject; darcs init; darcs add -r *. Use apt-get install darcs to install or see the darcs wiki for source, tools, and more info.

Watch, Again (Written by Thomas Materna)
Reader Thomas Materna says: If you use watch to monitor the output of a command that contains wildcards, enclose the command in quotes so the expansion is made every time. For example, watch "ls *.gz" shows new *.gz files as they're created, whereas watch ls *.gz only shows the *.gz files that exist at the moment you run watch.

A Smart Man (Written by Håkon Karsten Eide)
If you want to look at a compressed log file (.gz), use man. For example, man ./auth.log.1.gz shows the contents of the log file. (Thanks to Håkon Karsten Eide.)

Look at Files in Reverse
To look at a log in reverse (most recent events first), run tac logfile | less. tac reverses the lines of any file. Use tac - to read from stdin.

Move Wisely
If you're not careful, mv can overwrite existing files. Move files wisely with mv --backup=existing -v. To save typing, set VERSION_CONTROL to existing.

Bash Quick Copy (Written by Steve Fox)
In bash, the command-line incantation cp snark{,.bak} copies snark to snark.bak. (Thanks to Steve Fox.)

Command Redux
To run a command again and again and see what's different each time, use the clever watch. For example, watch -d ls -l runs ls -l every two seconds and displays "diffs."

Spying on "Noisy" Files
If you move files from other operating systems to Linux, the files may contain odd, unprintable characters. To reveal control characters, non-ASCII characters, and so on, use either cat -e filename or od -c filename.

Temporary Files Made Easy
Need a reliable way to choose a unique temporary file? Use tempfile. For instance, tempfile -p martin creates /tmp/martiOzXxwL with mode 0600.

Another Fast Backup (Written by Morvan Bliasby)
To make a fast backup, say, of all system .conf files, combine locate, grep, and zip as in locate .conf | grep /etc/ | zip zipfile -@

Create a Tarball
A so-called tarball is a gzip-compressed tar archive. You can create one quickly with tar zcvf tarball.gz files.... To unpack a tarball, use tar zxvf tarball.gz.

Are You Sure?
Many commands that affect the file system, such as ln, mv, and rm, have -interactive options (abbreviated by -i). Use -interactive mode to approve each operation.

Who, What, When?
Need to know what makes your machine tick? Just type uname -a to see your machine's hostname, kernel version number, processor type, and more. Also see the commands arch, hostname, env, kernelversion, and ifconfig.

Update Debian
If you use Debian Linux, don't forget to run apt-get update; apt-get upgrade at least once a week to pick up the latest and greatest packages and security patches.

Tune In the Tube (Written by Johan Eriksson)
Watch a lot of TV? Tired of missing Most Extreme Elimination Challenge? Reader Johan Eriksson recommends Maxemum TV-Guide, a free KDE application that browses the television broadcast schedules of several countries.

Add Users En Masse
You can quickly create new user accounts or update existing accounts with the newusers command. Create a list of accounts in a passwd-like file and run the command newusers file.

Tee for Two (or Three, Four, ...)
To watch the output of a command and capture the output in a file, use tee. For example, find / -name '*.pm' -print | tee log lets you watch the results and keep the results in log. Use –a to append.

Links in a Snap
Use cp -l to make links to existing files. You can even use cp -rl to create a shadow of an entire directory tree.

Modes Made Easy
In addition to octal numeric modes (such as 644), chmod also supports a symbolic mode that can set and incrementally add or remove permissions. For example, chmod g+w adds group write without changing other permissions, chmod o-a removes all permissions for other, while chmod u=rw gives read/write permission only to the file's owner (and is the equivalent of setting mode 600).

Customized File Metadata
Build your own view of file metadata (permissions, file name, file size, and so on) with the stat command. Use -c (or --format=) to display the fields you want, in the order you want -- perfect for processing with scripts!

Beware Root
Avoid logging in as root. Instead, use sudo to run individual commands as the superuser. As Spider-man says, "With great power comes great responsibility."
Typically Puppy runs in Root so there is no need to use sudo. Some versions have a default user called Spot.

Which Which is Which?
If you want to know all of the locations of a particular command in your PATH, use which -a command. The result is a list of filenames in PATH order.

Disable A User Account
To temporarily disable a user account, run password -l username. To re-enable an account, use the -u option.

Process Lookup Made Very Easy
Need to find or kill a process? Use the utilities pgrep and pkill. For example, the command pkill -INT -u root sshd sends the INT signal to the sshd process owned by root. For puppy 2.10, use menu/control panel/KP manage system processes for a graphical interface to all your processes.

Easy Parsing
If you have a comma-delimited file, use cut -s -d, -f n file to extract the nth field from each line of file. The -s option skips lines without the delimiter.

Less is Much More
Here's a way to save some typing: pipe the output of a command to less -Ofile, as in find / -name '*.pl' | less -Operlfiles. The option -Operlfiles automatically copies stdin to the file perlfiles.

Unique Temporary Filenames
Shell scripts often use temporary files to store transient data. To ensure the novelty of each temporary filename, append a qualifer such as $$, the current process ID, to the filename.

Compressed File Tools
View and process a gzip-compressed file (such as an archived log file) without expanding it using zcat, zdiff, zmore, zless, zcmp, zgrep, zegrep, and zfgrep.

Download from the Command-Line
wget is an invaluable utility. Use it from the command-line or a shell script to download a web page, an entire web site, a tarball, or just about any http: or ftp: URL. wget can also help debug HTTP and FTP connection problems.

Watch a Log File
If you want to watch a log file, use tail -F. The -F option prints new content as it's appended (like -f), but also re-opens a file if it's created anew, as occurs when a log file is rotated.

Perl One-Liner
You don't have to know Perl to use Perl. The command-line perl -p -i -e 's/oldstring/newstring/g' * replaces all occurrences of oldstring with newstring in all files in the current directory and makes backup copies of the original files.

Instant Environment Variable (Written by Jerry Peek)
If you use bash, you can set an environment variable for a single command by setting the variable before the command, like this: VISUAL=/usr/bin/emacs ci -l foo.txt

Copy a Directory
Need to make a verbatim copy of a directory? Use the command (cd /old/directory; tar cf - .) | (cd /new/directory; tar xf -)

Find Any File Fast
Need to find a system configuration file but only remember a part of its name? Use locate. It's faster than running find / ... and much simpler to use. (Run updatedb often to keep the locate file database up-to-date.)

Reference the Directory Stack
If you use pushd and popd to maintain a directory stack, use ~n on the command-line to reference the nth element of the stack with (where n is 0, 1, 2, and so on).

Run a Job at Boot
Written by Martin Streicher
If you want to run a command every time your machine boots, but don't have access to the init scripts, use cron's @reboot qualifier.

Also see

There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki