Customize your bash prompt

If you’re like me you always intend to customize your UNIX or Linux prompt but never seem to find the time to look up the options and make the change. Well, to give you a jump start here’s an excerpt from my book Easy Linux Commands.

In order to eliminate the need to frequently issue the pwd command to determine the current working directory, many Linux users choose to display the working directory within the Linux command prompt. Some Linux administrators even provide this service for their users when they create their Linux accounts. If a user’s command prompt does not contain the working directory, the command prompt can be modified by changing the prompt string 1 (PS1) shell variable as demonstrated here:

$ PS1="[\u@\h \w]\\$ "
[tclark@appsvr /home/tclark]$

This example of setting the PS1 variable also adds the username (\u) and hostname (\h) to the prompt. This can be very useful if you frequently connect to different hosts and as different users.

In order to avoid having to modify the prompt at each login, the following line of code can be placed within the appropriate configuration file, such as .bash_profile for the bash shell, within the home directory. To do this you can use a text editor like vi to add the following line to your .bash_profile file:

export PS1="[\u@\h \w]\\$ "

Note: Files that begin with a period will not appear when you list the contents of a directory. To see these hidden files use the command ls –a

There are even more options which you can put into your PS1 prompt. While it’s nice to keep your prompt fairly short you may find some of the other options useful. The following table contains a list of values that can be displayed within the PS1 and/or PS2 prompt strings:


Displayed Value


History number of current command


Command number of current command


Current date


Host name




Shell name


Current time


User name


Current working directory


Current working directory (full path)

As a bonus, here are a few of my favorite options for the PS1 prompt:

export PS1='\u@\h$ '

export PS1='\w$ '

export PS1='\d$ '
Mon Oct 30$

You may notice that all these have the dollar sign ($) in them which is typical of bash prompts. There is also a space after the dollar sign so you can easily tell where your prompt ends and your commands begin.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!

linux, unix, system administration, sysadmin, bash, shell, unix shell, shell prompt

Make directories and subdirectories simultaneously

Here’s a review of the mkdir command and the often overlooked -p option from Easy Linux Commands by Terry Clark and I.

Creating New Directories

The mkdir (make directory) command is used to create a new directory. The directory to be created can be referenced via an absolute path or via a relative path starting with the current working directory.

Make a directory using an absolute path:

$ ls
$ mkdir /home/tclark/new_dir1
$ ls
examples new_dir1

Make a directory using a relative path:

$ mkdir new_dir2
$ ls
examples new_dir1 new_dir2

The –p (parent) option allows creation of a complete directory branch when parent directories do not yet exist. This will actually create both the parent directory and subdirectory specified. In our example that means the new_dir3 and sub_dir3 are both created with a single command.

Make a new directory with a subdirectory using the –p option:

$ mkdir -p new_dir3/sub_dir3
$ ls
examples new_dir1 new_dir2 new_dir3
$ ls new_dir3

Of course you can also list more than one directory to create with a single mkdir command.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!

unix, linux, system administration, sysadmin

Displaying the Exit status of a UNIX or Linux command

Easy Linux Commands: Working Examples of Linux Command SyntaxFrom my upcoming book Easy Linux Commands:

Whenever a command or shell script completes successfully, it sets a hidden status code of zero. If the command is unsuccessful, it sets a nonzero hidden status code. This completion status code is known as the exit status. The exit status of the last command or script that was run is contained in the special shell variable, $?.

Most of the time we never look at this value and instead check to see if the command did what we want or look for errors in the output of commands. In a shell script, however, we may want to check the exit status to make sure everything is going OK. The exit status of the last command can be displayed as follows:

$ ls
example1.fil examples test.bsh umask_example.fil
$ echo $?
$ ls *.txt
ls: *.txt: No such file or directory
$ echo $?

The value of the exit code can then be used in a conditional statement or be transferred to another variable.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!

linux, unix, linux command, exit status

SSH Without A Password

Zach has posted a good quick reference for setting up SSH to use a shared key for authentication instead of a password on a UNIX system. It’s important to keep your keys secure, but this can allow you to set up scripts to execute commands or move files between multiple hosts without prompting for passwords.

If memory serves this type of authentication is enabled by default on most ssh servers, but if it doesn’t work talk to your sys-admin to see if it is disabled.

unix, linux, solaris, mac osx, osx, ssh, security

UNIX Find And Execute

Zach tells me I need to post how to find files and execute a command on those files. While the man pages are always the definitive reference, these are the options I use frequently. As usual, anything in italics should be replaced with the details for your search.

Note: WordPress loves to convert quotes into fancy quotes (pointing in from each side) so these commands may not work too well if copied and pasted. On the bright side, there’s no time like the present to start commiting these to muscle memory.

Find a file and execute a specific command:

find path conditions -exec command {} \;

find ./ -name "*.php" -exec cat {} \;

The open/close curly bracket marks where the name of each file found will be substituted and the backslash-semicolon marks the end of the command to execute.

Here are a few other options I use frequently:

Search for a file by name:

find path -name filename

find ./ -name style.css

Search by name with wildcards:

find path -name "filename"

find ./ -name "*.html"

Search for files modified within the past n days:

find path -mtime -n

find ./ -mtime -7

Search for files modified before the past n days:

find path -mtime +n

find /tmp -mtime +3

These are mostly based on my experience in Solaris using the bash shell. They should work just about everywhere, but check the man pages as mileage will vary.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!

unix, solaris, linux, mac osx, osx, sysadmin, systems administration