Linux Commands

rename

As you know, the mv command renames files. For example,

$ mv oldname newname
renames the file oldname to newname. However, what if you don’t know the filenames yet? The rename command comes in really handy here.

rename .log .log.‘date +%F-%H:%M:%S‘ *

replaces all files with the extension .log with .log.. So sqlnet.log becomes sqlnet.log.2006-09-12-23:26:28.

alias and unalias

Suppose you want to check the ORACLE_SID environment variable set in your shell. You will have to type:

echo $ORACLE_HOME

As a DBA or a developer, you frequently use this command and will quickly become tired of typing the entire 16 characters. Is there is a simpler way?

There is: the alias command. With this approach you can create a short alias, such as “os”, to represent the entire command:

alias os=’echo $ORACLE_HOME’

Now whenever you want to check the ORACLE_SID, you just type “os” (without the quotes) and Linux executes the aliased command.

However, if you log out and log back in, the alias is gone and you have to enter the alias command again. To eliminate this step, all you have to do is to put the command in your shell’s profile file. For bash, the file is .bash_profile (note the period before the file name, that’s part of the file’s name) in your home directory. For bourne and korn shells, it’s .profile, and for c-shell, .chsrc.

You can create an alias in any name. For instance, I always create an alias for the command rm as rm -i, which makes the rm command interactive.

alias rm=’rm -i’

Whenever I issue an rm command, Linux prompts me for confirmation, and unless I provide “y”, it doesn’t remove the file—thus I am protected form accidentally removing an important file. I use the same for mv (for moving the file to a new name), which prevents accidental overwriting of existing files, and cp (for copying the file).

Here is a list of some very useful aliases I like to define:

alias bdump=’cd $ORACLE_BASE/admin/$ORACLE_SID/bdump’
alias l=’ls -d .* –color=tty’
alias ll=’ls -l –color=tty’
alias mv=’mv -i’
alias oh=’cd $ORACLE_HOME’
alias os=’echo $ORACLE_SID’
alias rm=’rm -i’
alias tns=’cd $ORACLE_HOME/network/admin’

Painless Changes to Owner, Group, and Permissions

# ls -l
total 8
-rw-r–r– 1 test users 70 Aug 4 04:02 file1
-rwxr-xr-x 1 oracle dba 132 Aug 4 04:02 file2
-rwxr-xr-x 1 oracle dba 132 Aug 4 04:02 file3
-rwxr-xr-x 1 oracle dba 132 Aug 4 04:02 file4
-rwxr-xr-x 1 oracle dba 132 Aug 4 04:02 file5
-rwxr-xr-x 1 oracle dba 132 Aug 4 04:02 file6

and you need to change the permissions of all the files to match those of file1. Sure, you could issue chmod 644 * to make that change—but what if you are writing a script to do that, and you don’t know the permissions beforehand? Or, perhaps you are making several permission changes and based on many different files and you find it infeasible to go though the permissions of each of those and modify accordingly.

A better approach is to make the permissions similar to those of another file. This command makes the permissions of file2 the same as file1:

chmod –reference file1 file2

Now if you check:

# ls -l file[12]
total 8
-rw-r–r– 1 test users 70 Aug 4 04:02 file1
-rw-r–r– 1 oracle dba 132 Aug 4 04:02 file2

The file2 permissions were changed exactly as in file1. You didn’t need to get the permissions of file1 first.

You can also use the same trick in group membership in files. To make the group of file2 the same as file1, you would issue:

# chgrp –reference file1 file2
# ls -l file[12]
-rw-r–r– 1 test users 70 Aug 4 04:02 file1
-rw-r–r– 1 oracle users 132 Aug 4 04:02 file2

Of course, what works for changing groups will work for owner as well. Here is how you can use the same trick for an ownership change. If permissions are like this:

# ls -l file[12]
-rw-r–r– 1 test users 70 Aug 4 04:02 file1
-rw-r–r– 1 oracle dba 132 Aug 4 04:02 file2

You can change the ownership like this:

# chown –reference file1 file2
# ls -l file[12]
-rw-r–r– 1 test users 70 Aug 4 04:02 file1
-rw-r–r– 1 test users 132 Aug 4 04:02 file2

Note that the group as well as the owner have changed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: