cancel
Showing results for 
Search instead for 
Did you mean: 

usefull unix commands

Former Member
0 Kudos

Dear guru,s

In our workplace sap-ecc5.0,db-oracle,OS-HP_UX .Just i wants to know what are the commands we can use at os level...........for seeing file system,giving file permission,ect.Please anyone send me those commands which are we can use..............

Regard's

rocky

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

Hi,

Linux Commands for working with Files

1. Listing the files

ls

Lists the contents of a directory.

Syntax

ls [-a] [-A] [-b] [-c] [-C] [-d] [-f] [-F] [-g] [-i] [-l] [-L] [-m] [-o] [-p] [-q] [-r] [-R] [-s] [-t] [-u] [-x]

-a Shows you all files, even files that are hidden (these files begin with a dot.)

-A List all files including the hidden files. However, does not display the working directory (.) or the parent directory (..).

-d If an argument is a directory it only lists its name not its contents.

-r Reverses the order of how the files are displayed.

-R Includes the contents of subdirectories.

-s Give size in blocks, including indirect blocks, for each entry.

-t Shows you the files in modification time.

Examples

ls -l

In the above example this command would list each of the files in the current directory and the files permissions, the size of the file, date of the last modification, and the file name or directory.

Permissions Directories Group Size Date Directory or file

drwx------ 2 users 4096 Nov 2 19:51 mail/

drwxr-s--- 35 www 32768 Jan 20 22:39 public_html/

-rw------- 1 users 3 Nov 25 02:58 test.txt

ls /

List the contents of your root directory.

ls ../

List the contents of the parent directory.

ls */

List the contents of all sub directories.

ls -d */

Only list the directories in the current directory.

2. Displaying the files

cat

Allows you to look, modify or combine a file.

Syntax

cat filename [-n] [-b] [-u] [-s] [-v]

filename The name of the file or files that you wish to look at or perform tasks on.

-n Precede each line output with its line number.

Examples

cat file1.txt file2.txt > file3.txt - Reads file1.txt and file2.txt and combines those files to make file3.txt.

3. Copying/Renaming Files

cp

Copies files from one location to another.

Syntax

cp [OPTION]... SOURCE DEST

cp [OPTION]... SOURCE... DIRECTORY

-i, --interactive prompt before overwrite

-l, --link link files instead of copying

-p same as --preserve=mode,ownership,timestamps

-R, -r, --recursive copy directories recursively

-u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing

Examples

cp file1.txt newdir

Copies the file1.txt in the current directory to the newdir directory.

cp *.txt newdir

Copy all files ending in .txt into the newdir directory.

cp -r /home/hope/files/* /home/hope/backup

Copies all the files, directories, and subdirectories in the files directory into the backup directory.

4. Renaming Files

mv - move (rename) files

Syntax

mv [OPTION]... SOURCE DEST

mv [OPTION]... SOURCE... DIRECTORY

-f, --force

do not prompt before overwriting equivalent to --reply=yes

-u, --update

move only when the SOURCE file is newer than the destination file or when the destination file is missing

5. Creating and deleting Files

cd

Changes the directory.

Syntax

cd [directory]

directory Name of the directory user wishes to enter.

cd .. Used to go back one directory on the majority of all Unix shells. It is important that the space be between the cd and the ..

Examples

cd hope

The above example would go into the hope directory if it exists.

cd ../home/users/computerhope

The above example would go back one directory and then go into the home/users/computerhope directory.

cd ../../

the above example would go back two directories.

cd

typing just cd alone will move you into the home directory.

mkdir

Short for make directory this command is used to create a new directory.

Syntax

mkdir dirname

Examples

mkdir mydir - This would create a new directory called mydir.

Deleting Files

rm

Deletes a file without confirmation (by default).

Syntax

rm [-f] [-i] [-R] [-r] [filenames | directory]

-f Remove all files (whether write-protected or not) in a directory without prompting the user. In a write-protected directory, however, files are never removed (whatever their permissions are), but no messages are displayed. If the removal of a write-protected directory is attempted, this option will not suppress an error message.

-i Interactive. With this option, rm prompts for confirmation before removing any files. It over- rides the -f option and remains in effect even if the standard input is not a terminal.

-R Same as -r option.

-r Recursively remove directories and subdirectories in the argument list. The directory will be emptied of files and removed. The user is normally prompted for removal of any write-protected files which the directory contains. The write-protected files are removed without prompting, however, if the -f option is used, or if the standard input is not a terminal and the -i option is not used.

Examples

rm myfile.txt

Remove the file myfile.txt without prompting the user.

rm -r directory

Remove a directory, even if files existed in that directory.

rmdir

Deletes a directory.

Syntax

rmdir [OPTION]... DIRECTORY...

Examples

rmdir mydir - removes the directory mydir

rm -r directory - would remove a directory, even if files existed in that directory

6. File permission commands

chmod

Changes the permission of a file.

Syntax

chmod [OPTION]... MODE[,MODE]... FILE...

-R, --recursive change files and directories recursively

Permissions

u - User who owns the file.

g - Group that owns the file.

o - Other.

a - All.

r - Read the file.

w - Write or edit the file.

x - Execute or run the file as a program.

Numeric Permissions:

CHMOD can also to attributed by using Numeric Permissions:

400 read by owner

040 read by group

004 read by anybody (other)

200 write by owner

020 write by group

002 write by anybody

100 execute by owner

010 execute by group

001 execute by anybody

Examples

The above numeric permissions can be added to set a certain permission, for example, a common HTML file on a Unix server to be only viewed over the Internet would be:

chmod 644 file.htm

This gives the file read/write by the owner and only read by everyone else (-rw-rr).

Files such as scripts that need to be executed need more permission. Below is another example of a common permission given to scripts.

chmod 755 file.cgi

This would be the following 400040004200020100010+001 = 775 where you are giving all the rights but the capability for anyone to edit your file.cgi (-rwxr-xr-x).

Finally, another common CHMOD permission is 666, as shown below, which is read and write by everyone.

chmod 666 file.txt

chown

Command for that changes the owner of a file.

Syntax

chown [-R] newowner filenames

-R Change the permission on files that are in the subdirectories of the directory that you are currently in.

Examples

chown xyz file.txt - Give permissions as owner to user xyz.

Set User Identification Attribute (setuid)

When the set user ID access mode is set in the owner permissions, and the file is executable, processes which run it are granted access to system resources based on the owner of the file.

Be extremely careful when setting these permissions. Any user who runs that file assumes the permissions of the owner of the executable file, instead of the user who created the process..

The setuid permission is shown as an s in the file permissions. For example, the setuid permission on the /usr/bin/passwd command enables normal users to read and write an otherwise inaccessible /etc/passwd file:

user@myhost $ ls -l /etc/shadow /etc/passwd /usr/bin/passwd

-r----


1 root root 659 Jul 25 19:40 /etc/shadow

-rw-rr 1 root root 711 Jul 25 19:40 /etc/passwd

-r-sr-xr-x 1 root bin 15613 Apr 27 12:29 /usr/bin/passwd

You will notice that the s takes the place of the execute bit in the example above. This special permission mode really has no meaning unless the file also has execute permission as well.

In the example we see the /etc/shadow file is only readable by root, yet the /usr/bin/passwd file enables us to write our password changes there. When either a normal user, a member of the bin group, or even anyone else executes /usr/bin/passwd, it is really run as root, due to the ``s'' bit set in the owner's permissions field.

It is advisable to keep setuid, in order to reduce the possibility of their being exploited.

Umask Settings

The umask command is used to set and determine the default file creation permissions on the system. It is the octal complement of the desired file mode for the specific file type. Default permissions are:

• 777 - Executable files

• 666 - Text files

These defaults are set allowing all users to execute an executable file and not to execute a text file. The defaults allow all users can read and write the file.

The permission for the creation of new executable files is calculated by subtracting the umask value from the default permission value for the file type being created. An example for a text file is shown below with a umask value of 022:

666 Default Permission for text file

-022 Minus the umask value

-


644 Allowed Permissions

Therefore the umask value is an expression of the permissions the user, group and world will not have as a default with regard to reading, writing, or executing the file. The umask value here means the group the file belongs to and users other than the owner will not be able to write to the file. In this case, when a new text file is created it will have a file permission value of 644, which means the owner can read and write the file, but members of the group the file belongs to, and all others can only read the file. A long directory listing of a file with these permissions set is shown below.

-rw-rr 1 root workgrp 14233 Apr 24 10:32 textfile.txt

An example command to set the umask is:

umask 022

The most common umask setting is 022. The /etc/profile script is where the umask command is usually set for all users.

7. Searching for files

find

Finds one or more files

Syntax

find path expressions

-mtime n True if the file's data was modified n days ago.

-name pattern True if pattern matches the current file name. Normal shell file name generation characters (see sh) may be used. A backslash (\) is used as an escape character within the pattern. The pattern should be escaped or quoted when find is invoked from the shell.

-size n[c] True if the file is n blocks long (512 bytes per block). If n is followed by a c, the size is in

bytes.

When using find to determine files modified within a range of time, one must use the ?time argument before the -print argument; otherwise, find will give all files.

Examples

find -name 'mypage.htm'

In the above command the system would search for any file named mypage.htm in the current directory and any subdirectory.

find / -name 'mypage.htm'

In the above example the system would search for any file named mypage.htm on the root and all subdirectories from the root.

find -name 'file*'

In the above example the system would search for any file beginning with file in the current directory and any subdirectory.

find -name '*' -size +1000k

In the above example the system would search for any file that is larger then 1000k.

8. Linking files

Hard Links and Symbolic Links

They are listings that contain information about the file. Linux files don't actually live in directories. They are assigned an inode number, which Linux uses to locate files. So a file can have multiple hardlinks, appearing in multiple directories, but isn't deleted until there are no remaining hardlinks to it. Here are some other differences between hardlinks and symlinks:

1. You cannot create a hardlink for a directory.

2. If you remove the original file of a hardlink, the link will still show you the content of the file.

3. A symlink can link to a directory.

4. A symlink, like a Windows shortcut, becomes useless when you remove the original file.

Hardlinks

$ ln FileA FileB

Then use the "i" argument to list the inodes for both FileA and its hardlink. Type:

$ ls -il FileA FileB

This is what you get:

1482256 -rw-rr 2 bruno bruno 21 May 5 15:55 FileA

1482256 -rw-rr 2 bruno bruno 21 May 5 15:55 FileB

You can see that both FileA and FileB have the same inode number (1482256). Also both files have the same file permissions and the same size. Because that size is reported for the same inode, it does not consume any extra space on your HD!

Next, remove the original FileA:

$ rm FileA

And have a look at the content of the "link" FileB:

$ cat FileB

You will still be able to read the file..

Symlinks

Let's make a symlink to FileB. Call the symlink FileC:

$ ln -s FileB FileC

Then use the i argument again to list the inodes.

$ ls -il FileB FileC

This is what you'll get:

1482256 -rw-rr 1 bruno bruno 21 May 5 15:55 FileB

1482226 lrwxrwxrwx 1 bruno bruno 5 May 5 16:22 FileC -> FileB

You'll notice the inodes are different and the symlink got a "l" before the rwxrwxrwx. The link has different permissions than the original file because it is just a symbolic link. Its real content is just a string pointing to the original file. The size of the symlink (5) is the size of its string. (The "-> FileB" at the end shows you where the link points to.)

Now if we remove the original file:

$ rm FileB

and check the Test directory:

$ ls

You'll see the symlink FileC is still there, but if you try to list the contents:

$ cat FileC

It will tell you that there is no such file or directory. You can still list the inode. Typing:

$ ls -il FileC

will still give you:

1482226 lrwxrwxrwx 1 bruno bruno 5 May 5 16:22 FileC -> FileB

But the symlink is obsolete because the original file was removed, as were all the hard links. So the file was deleted even though the symlink remains.

9. Grep/egrep

grep

Finds text within a file.

Syntax

grep [options] [FILE...]

Patterns for searching.

. Matches single character.

* Wild character Example C* if found would pull up CC or CAT...

{} Matches any character contained within the bracket.

^ Represents the beginning of the line, so if you did ^T it would search for any sentence starting with a T.

$ Represents the end of the line, so if you did $. then it would pull up any lines that ended with .

\ Means to take the next character serious so you could search for C\ C.

Examples

grep "unix" *.htm

search all .htm files in the current directory for any reference of unix and give results similar to the below example text.

egrep

Search a file for a pattern using full regular expressions.

Syntax

egrep [-b] [-c] [-h] [-i] [-l] [-n] [-s] [-v] [-x] [ -e pattern_list ] [-f file ] [ strings ] [ file]

Examples

egrep "support|help|windows" myfile.txt - Would search for patterns of support help and windows in the file myfile.txt.

10. su command

Become super user or another user.

Syntax

su [ - ] [ username [ arg ] ]

username The name of another username that you wish to log in as.

arg Additional arguments that need to be passed through the su command.

Examples

su - hope - would login as the user hope .

11. Xargs

xargs

Build and execute command lines from standard input.

Syntax

xargs [-0prtx] [-e[eof-str]] [-i[replace-str]] [-l[max-lines]] [-n max-args] [-s max-chars] [-P max-procs] [null] [eof[=eof-str]] [replace[=replace-str]] [max-lines[=max-lines]] [interactive] [max-chars=max-chars] [--verbose]

Examples

find . -type f -mtime -1 -print | xargs pr -n

Find any files that have a modified time of 1 day and print the contents of each of those files.

Regards,

Raghu

Answers (3)

Answers (3)

Former Member
0 Kudos

Basic UNIX commands

Note: not all of these are actually part of UNIX itself, and you may not find them on all UNIX machines. But they can all be used on turing in essentially the same way, by typing the command and hitting return. Note that some of these commands are different on non-Solaris machines - see SunOS differences.

If you've made a typo, the easiest thing to do is hit CTRL-u to cancel the whole line. But you can also edit the command line (see the guide to More UNIX).

UNIX is case-sensitive.

Files

$ cat > filename

How r u?

Cntl+z to save.

$ cat < filename

To view file

File Management

emacs: `Using the emacs text editor'

mkdir: `Creating a directory'

cd: `Changing your current working directory'

ls: `Finding out what files you have'

cp: `Making a copy of a file'

mv: `Changing the name of a file'

rm: `Getting rid of unwanted files'

chmod: `Controlling access to your files'

cmp: Comparing two files

wc: Word, line, and character count

compress: Compress a file

GabrielSagaya
Active Contributor
0 Kudos

File System Navigation

cd

Return to your home directory.

cd directory

Change directory to make directory your current directory.

file files

Determine file type.

ls

List the contents of the current directory.

ls names

List the contents of the directories; names can name files and/or directories:

ls -l

. . . in a long format, showing permissions, owner, size, and other file info.

ls -a

. . . all files, including "hidden" files (file names that begin with a dot ".").

ls -R

. . . Recursively, for all subdirectories.

ls -t

. . . in time order (when modified, newest to oldest) rather than in name order.

pwd

Display the name of the current directory, or "print working directory."

$ ls -l

drwxr-xr-x 4 cliff user 1024 Jun 18 09:40 WAITRON_EARNINGS

-rw-rr 1 cliff user 767392 Jun 6 14:28 scanlib.tar.gz

^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^

| | | | | owner group size date time name

| | | | number of links to file or directory contents

| | | permissions for world

| | permissions for members of group

| permissions for owner of file: r = read, w = write, x = execute -=no permission

type of file: - = normal file, d=directory, l = symbolic link, and others...

Changing file permissions and attributes

chmod 755 file Changes the permissions of file to be rwx for the owner, and rx for

the group and the world. (7 = rwx = 111 binary. 5 = r-x = 101 binary)

chgrp user file Makes file belong to the group user.

chown cliff file Makes cliff the owner of file.

chown -R cliff dir Makes cliff the owner of dir and everything in its directory tree.

You must be the owner of the file/directory or be root before you can do any of these things.

http://www.itd.umich.edu/itcsdocs/r1159/

http://freeengineer.org/learnUNIXin10minutes.html

Former Member
0 Kudos

Hi,

You could use commands from below links

http://www-d0.fnal.gov/~jkrane/unix.html

http://www.simotime.com/ftp4cmd1.htm

Thanks

Swarup