UNIX BASICS - Beginners guide for Unix commands - by Meenakshi

For more software testing articles visit: http://www.softwaretestinghelp.com

Main features of unix :
Multi user - More than one user can use the machine Multitasking- More than one program can be run at a time. Portability – This means the operating system can be easily converted to run on different browsers.

ls when invoked without any arguments, lists the files in the current working directory. A directory that is not the current working directory can be specified and ls will list the files there. The user also may specify any list of files and directories. In this case, all files and all contents of specified directories will be listed. Files whose names start with "." are not listed, unless the -a flag is specified or the files are specified explicitly. Without options, ls displays files in a bare format. This bare format however makes it difficult to establish the type, permissions, and size of the files. The most common options to reveal this information or change the list of files are: long format, displaying Unix file type, permissions, number of hard links, owner, group, size, date, and filename

appends a character revealing the nature of a file, for example, * for an executable, or / for a directory. Regular files have no suffix.

lists all files in the given directory, including those whose names start with "." By default, these files are excluded from the list.
-a -R

recursively lists subdirectories. The command ls -R / would therefore list all files.


Common options that rm accepts include: -r. \ in DOS) or move to any given directory. rm (short for remove) is a Unix command used to delete files from a filesystem. and bash. cd is frequently included built into certain shells such as the Bourne shell. which processes subdirectories recursively -i. pwd command (print working directory) is used to print the name of current working directory from a computer's command-line interface. tcsh. Example: $ pwd /home/foobar mkdir command in the Unix operating system is used to make a new Directory. The cd command can be used to change into a subdirectory. which ignores non-existent files and overrides any confirmation prompts ("force") . It is also available for use in Unix shell scripts or DOS batch files. bash (where it calls the chdir() POSIX C function) and in DOS's COMMAND. move all the way back to the root (/ in UNIX. the new directory would be created within the current directory.COM. The DOS equivalent is "CD" with no arguments. When typed as above (ie. A directory is a logical section of a filesystem used to hold files. the user can use this command to find their place in the directory tree. This command is found in the Unix family of operating systems and other flavors as well.Is a command line command used to change the current working directory in the Unix and DOS operating systems. move back into the parent directory. normal usage). Normal usage is as straightforward as follows: mkdir name_of_directory Where name_of_directory is the name of the directory one wants to create. It can be implemented easily with the POSIX C functions getcwd() and/or getwd(). If the shell prompt does not already show this. which asks for every deletion to be confirmed -f. Directories may also contain other directories. It is a command which is sometimes included built into certain shells such as sh.

For example: rmdir -Rf for/bar/baz cp is the command entered in a Unix shell to copy a file from one place to another. "rm -rf" (variously. The "rm -rf /" variant of the command.] SourceFile .] { -r | -R } SourceDirectory . It cannot be capitalized. they can manually cancel out the -i argument by adding the -f option. TargetDirectory To Copy a Directory to a Directory cp [ -f ] [ -h ] [ -i ] [ -p ] [ -. "rm -rf *". Often rmdir will not remove a directory if there is still files present in the directory. and others) is frequently used in jokes and anecdotes about Unix disasters. if run by an administrator. .rm is often aliased to "rm -i" so as to avoid accidental deletion of files. The original file remains unchanged. "rm -rf /". To Copy a File to another File cp [ -f ] [ -h ] [ -i ] [ -p ][ -. If a user still wishes to delete a large number of files without confirmation. For example: rmdir –p foo/bar/baz Will first remove baz/... rmdir is a command which will remove an empty directory on a Unix-system... would cause the contents of every mounted disk on the computer to be deleted.] SourceFile TargetFile To Copy a File to a Directory cp [ -f ] [ -h ] [ -i ] [ -p ] [ -r | -R ] [ -. To force the removal of the directory even if files are present usually the -rf flag can be used. then bar/ and finally foo/ thus removing the entire directory tree specified in the command argument. possibly on a different filesystem. and the new file may have the same or a different name. There are options to this command such as -p which removes parent directories if they are also empty. TargetDirectory -f (force) – specifies removal of the target file if it cannot be opened for write operations. Normal usage is straightforward where one types: rmdir name_of_directory Where name_of_directory corresponds with the name of the directory one wishes to delete.

including all its files and subdirectories.jr file the same date and time as the smith file. including all its files. If the prog. If you enter y or the locale's equivalent of y.jr file.bak. -p (preserve) – duplicates the following characteristics of each SourceFile/SourceDirectory in the corresponding TargetFile and/or TargetDirectory: Examples To make a copy of a file in the current directory. enter: cp /home/janet/clients/* /home/nick/customers This copies only the files in the clients directory to the customers directory. to another directory. enter: cp -p smith smith. to copy files to which symbolic links point. enter: cp jones /home/nick/clients This copies the jones file to /home/nick/clients/jones. enter: cp prog. To copy a file to a new file and preserve the modification date.c file. that is. If it does exist.The removal precedes any copying performed by the cp command. To copy a file in your current directory into another directory.jr file also inherits the smith file's access control protection. the cp command creates it. Instead of creating the file with the current date and time stamp. This occurs if the TargetDirectory or TargetFile parameter contains a file with the same name as a file specified in the SourceFile or SourceDirectory parameter. subdirectories. enter: cp jones lewis smith /home/nick/clients This copies the jones. to the customers/clients directory. lewis. the cp command replaces it with a copy of the prog. and access control list associated with the source file. -h – makes the cp command copy symbolic links. the system gives the smith. To copy all the files in a directory to a new directory.jr This copies the smith file to the smith. To copy a specific set of files to another directory. the cp command continues. time.c to prog. Any other answer prevents the cp command from overwriting the file. The default is to follow symbolic links.bak This copies prog. -i (interactive) – prompts you with the name of a file to be overwritten. enter: cp -R /home/nick/clients /home/nick/customers This copies the clients directory. and smith files in your current working directory to the . The smith. and the files in those subdirectories.bak file does not already exist. To copy a directory.c prog.

) find . Further. By default. find allows the user to specify an action to be taken on each matched file. You must type a space between the c and the final dot. It searches through a directory tree of a filesystem. The backslash before the star is needed to avoid the shell expansion. therefore excluding directories. for files and directories with names starting with my. enter: cp programs/*. (Note that early versions of the find command had no default action at all. -name my\* This searches in the current directory (represented by a period) and below it. the shell would replace my* with the list of files whose names begin with my in the current directory. -name "my*" -type f -ls This prints an extended file information. find executes the 'print' action. (dot). signified by the single . it is an extremely powerful program for applying actions to many files. therefore the resulting list of files would be discarded. mostly found on Unix-like platforms. Thus. Without the backslash. -name "my*" -type f This limits the results of the above search to only regular files. pipes. to the bewilderment of naïve users. symbolic links. An alternative is to enclose the the arguments in quotes: find . etc.c to the current directory. find returns all files below the current working directory. find program is a search utility. . -name "my*" Files only find .c . special files. by default. locating files based on some user-specified criteria. To use pattern-matching characters to copy files. This copies the files in the programs directory that end with . my* is enclosed in quotes as otherwise the shell would replace it with the list of files in the current directory starting with my Commands The previous examples created listings of results because. Examples From current directory find ./home/nick/clients directory. It also supports regexp matching.

touch echo is a command in Unix (and by extension. the current directory is assumed. ( -name "*jsp" -or -name "*java" ) -type f -ls This prints extended information on any file whose name ends with either 'jsp' or 'java'. Find any one of differently named files find . Specify a directory find /home/brian -name "myfile" -type f -print This searches for files named myfile in the /home/brian directory. Note that the parentheses are required. its descendants. One option makes it possible to set the file's time-stamp to something other than the current system date and time. It is generally not a good idea to look for data files this way. to prevent them from being interpreted as special shell characters. so it is best to specify the directory more precisely.and time-stamp. its access and modification time-stamps are set to the system's current date and time. which is the home directory for the user brian. which may vary from one Unix to another. "\(" and "\)". Also note that the operator "or" can be abbreviated as "o". If no file path is specified. . The command-syntax is: touch [options] <file_name> If the file exists. The "and" operator is assumed where no operator is given. If the file does not exist. can be invoked with options to change its behaviour. This can take a considerable amount of time. touch is a program on Unix and Unix-like systems used to change a file's date. It can also be used to create an empty file.Search all directories find / -name "myfile" -type f -print This searches every file on the computer for a file with the name myfile. such as Linux) and MS-DOS that places a string on the terminal. as if the file had been changed. It is typically used in shell scripts and batch programs to output status text to the screen or a file. an empty file of that name is created with its access and modification time-stamps set to the system's current date and time. You should always specify the directory to the deepest level you can remember. To touch a file simulates a change to the file. In many shells the parentheses must be escaped with a backslash. but this action is normally restricted to the owner of the file or the system's superuser.

du In Linux. Example output user19 user28 pts/35 pts/27 Apr 18 08:40 Apr 18 09:50 (localhost) (localhost) du (abbreviated from disk usage) is a Unix computer program to display the amount of disk space used under a particular directory or files on a file system.txt This is a test. Example: cat filename who The Unix command who displays a list of users who are currently logged into a computer. it is a part of the GNU Coreutils package. which provides the same information but also displays additional data and statistics. cat program concatenates the contents of files./test. counts the disk space by walking the directory tree. $ echo "This is a test. As such. The who command is related to the command w. reading from a list of files and/or standard input in sequence and writing their contents in order to standard output.txt $ cat . $du -k /seclog 4 /seclog/lost+found . rather than the default of 512 byte blocks. the amount of space on a file system shown by du may vary from that shown by df if files have been deleted but their blocks not yet freed. Example The -k flag will show the sizes in 1K blocks. cat takes the list of files as arguments but also interprets the argument "-" as standard input.$ echo This is a test. This is a test. The du utility first appeared in version 1 of AT&T UNIX. such as the length of time a particular user has been connected or what pseudo-teletype a user is connected to./test." > . The command accepts various options that vary by system to further specify the information that is returned.

com/sitemap/ QA/Testing Job openings: http://www. For more software testing resources visit following pages: Software Testing articles: http://www.softwaretestinghelp.softwaretestinghelp.com .softwaretestinghelp.softwaretestinghelp.com/jobs/ Software Testing Help: http://www.com/ Copyright 2008: http://www.132 /seclog/backup/aix7 136 /seclog/backup 44044 /seclog/temp 439264 /seclog We will discuss more advanced commands in the next article.

Sign up to vote on this title
UsefulNot useful