Preventing Accidental File Deletion in Linux and UNIX

A little trick that some Linux users like to implement to prevent accidental file deletion is via the use of an alias. Aliases are similar to variables and can either be set in a session or by placing the alias command in the .profile or .bash_profile file with a text editor.

By adding this alias the user will be prompted to confirm each file before it is deleted; otherwise Linux, unlike Microsoft Windows, will delete whatever files match the filename criteria without warning!

$ alias rm='rm -i'
$ touch touch1.fil touch2.fil touch3.fil
$ rm touch*
rm: remove regular empty file `touch1.fil'? y
rm: remove regular empty file `touch2.fil'? y
rm: remove regular empty file `touch3.fil'? y

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

Jumping on the Linux Bandwagon

In an interesting chain reaction Oracle and Microsoft have both recently committed to supporting Linux distributions.

Recently Oracle announced they will offer RedHat Linux support at very competitive rates. Beyond offering some healthy competition for RedHat support Oracle’s commitment also makes Oracle on Linux a single vendor solution for software support.

Microsoft was not far behind in announcing a partnership with Novel for Suse Linux sales support.

Only time will tell what this will do to the Unix/Unix-like OS market but it’s sure to shake things up in the short term.

linux, unix, microsoft, redhat, system administration, oracle, database administration, dba, sysadmin

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:

Symbol

Displayed Value

\!

History number of current command

\#

Command number of current command

\d

Current date

\h

Host name

\n

Newline

\s

Shell name

\t

Current time

\u

User name

\W

Current working directory

\w

Current working directory (full path)

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

Prompt:
export PS1='\u@\h$ '
Result:
oracle@glonk$

Prompt:
export PS1='\w$ '
Result:
/usr/local$

Prompt:
export PS1='\d$ '
Result:
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
examples
$ 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
sub_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 example2.xxx examples test.bsh umask_example.fil
$ echo $?
0
$ ls *.txt
ls: *.txt: No such file or directory
$ echo $?
1

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