Working with Files and Directories
To display the current working directory:
pwd command: It displays the current working directory.
Syntax: ls -options <DirName>|<FileName>
The options are discussed as follows:
Option |
Description
|
p | It lists all the files & directories. The directory names are succeeded by the symbol ‘/’ |
F | It lists all files along with their type. The symbols ‘/’, ‘*’, (None), ‘@’ at the end of file name represents directory, executable, Plain text or ASCII file & symbolic link respectively |
a | It lists all the files & directories name including hidden files |
l | It lists detailed information about files & directories |
t | It displays all the files & directories in descending order of their modified time. |
r | It displays all the files & directories in reverse alphabetical order |
R | It displays all the files & directories & sub-directories in recursive order |
i | It displays the inode number of files & directories |
tr | It displays all the files & directories in the ascending order of their last modified date |
ls -l → It list all the files and directories long list with the permission and other information. The output looks as follows:
Entry
|
Description
|
FileType | ‘-‘ for file & ‘d’ for directory |
Permissions | Permissions are in order of Owner, Group & Other |
LinkCount | Number of links to the file |
UID | Owner’s User ID |
GID | Group’s ID |
Size | Size of the file/directory |
Last ModifiedDate & ModifiedTime | Last Modified Date & Time of the file/directory |
<File/Directory Name> | File/Directory name |
Understanding permissions:
Following table explains the permission entry:
Entry
|
Description
|
– | No permission/denied |
r | read permission |
w | write permission |
x | execute permission |
Example:
# file data
data: English text
If cd command is used without any option it changes the directory from current working directory to user’s home directory.
Example: Let the user be ‘ravi’ and current working directory is /var/adm/messages#pwd
/var/adm/messages
#cd
#pwd
#/export/home/raviThere is also a different way to navigate to the user’s home directory :
#pwd
/var/adm/messages
#cd ~ravi
#pwd
/export/home/ravi
#cd ~raju
#pwd
/export/home/raju
#cd ~ravi/dir1
#pwd
/export/home/ravi/dir1In the above examples, the ‘~’ character is the abbreviation that represents the absolute path of the user’s home directory. However this functionality is not available in all shells.There are few other path name abbreviations which we can use as well. These are listed below :
. → current working directory
.. → Parent directory or directory above the current working directory.
So if we want to go to the parent directory of the current working directory following command is used:
#cd ..We can also navigate multiple levels up in directory using cd, .. and /.
Example: If you want to move two levels up the current working directory, we will use the command :
#cd ../..
#pwd
/export/home/ravi
#cd ../..
#pwd
/export
#cd ..
#pwd
/
cat command: It displays the entire content of the file without pausing.
Syntax: cat <file name>
Example:
data: English text
#cat data
This is an example for demonstrating the cat command.
#
Warning: The cat command should not be used to open a binary file as it will freeze the terminal window and it has to be closed. So check the file type using ‘file’ command, if you are not sure about it.more command: It is used to view the content of a long text file in the manner of one screen at a time.
Syntax: more <file name>The few scrolling options used with more command are as follows :
Scrolling Keys
|
Action
|
Space Bar | Moves forward one screen |
Return | Scrolls one line at a time |
b | Moves back one screen |
h | Displays a help menu of features |
/string | searches forward for a pattern |
n | finds the next occurrence of the pattern |
q | quits and returns to shell prompt |
Head and Tail command in UNIX/LINUX/SOLARIS:
head command: It displays the first 10 lines of a file by default. The number of lines to be displayed can be changed using the option -n. The syntax for the head command is as follows:
Syntax: head -n <file name>
This displays the first n lines of the file.
tail command: It displays the last 10 lines of a file by default. The number of lines to be displayed can be changed using the options -n or +n.
Syntax:
#tail -n <file name>
#tail +n <file name>
The -n option displays the n lines from the end of the file.
The +n option displays the file from line n to the end of the file.
The following option can be used with wc command:
Option
|
Description
|
l | Counts number of lines |
w | Counts number of words |
m | Counts number of characters |
c | Counts number of bytes |
This is an example for demonstrating the cat command.
Syntax:cp -option(s) surce(s) destination
Option
|
Description
|
i | Prevents the accidental overwriting of existing files or directories |
r | Includes the contents of a directory, including the contents of all sub-directories, when you copy a directory |
#cp file1 file2 dir1
In the above example file1 and file2 are copies to dir1.
mv command: It can be used to
#mv file1 file2 /var
#mv file1 file2
The mv command does not affect the contents of the files or directories being moved or renamed.
touch Command : It is used to create an empty file. We can create multiple file using this command.
Example: #touch file1 files2 file3
mkdir command : It is used to create directories.
Syntax: mkdir -option <dir name>
When the <dir name> includes a pah name, option -p is used to create all non-existing parent directory.
#mkdir -p /export/home/ravi/test/test1
rm command: It is used permanently remove files/directories.
Syntax: rm -option <file name>/<dir name>
The -i option is used to prompt user for confirmation before the deletion of files/directories.
#
To remove an empty directory:
Syntax: rmdir <directory name>
Searching Files, Directories & its contents:
Using the grep command : The grep is very useful and widely used command.
lets take an example where we want to see if the process statd is running of not. Following command is used :
#ps -ef | grep statd
# ps -ef | grep statd
daemon 2557 1 0 Jul 07 ? 0:00 /usr/lib/nfs/statd
root 10649 1795 0 05:29:39 pts/4 0:00 grep statd
#
Syntax: grep options filenames.
The options used are discussed below :
i | Searches both uppercase and lowercase characters |
l | Lists the name of files with matching lines |
n | Precedes each line with the relative line number in the file |
v | Inverts the search to display lines that do not match pattern |
c | Counts the lines that contain pattern |
w | Searches for the expression as acomplete word, ignoring those matches that are sub strings of larger words |
Lets see few examples:
Suppose we want to search for all lines that contain the keyword root in /etc/group file and view their line numbers, we use following option :
# grep -n root /etc/group
1:root::0:
2:other::1:root
3:bin::2:root,daemon
4:sys::3:root,bin,adm
5:adm::4:root,daemon
6:uucp::5:root
7:mail::6:root
8:tty::7:root,adm
9:lp::8:root,adm
10:nuucp::9:root
12:daemon::12:root
To search for all the lines that does not contain the keyword root:
# grep -v root /etc/group
staff::10:
sysadmin::14:
smmsp::25:
gdm::50:
webservd::80:
postgres::90:
unknown::96:
nobody::60001:
noaccess::60002:
nogroup::65534:
cta::101:
rancid::102:
mysql::103:
torrus::104:
To search for the names of the files that contains the keyword root in /etc directory :
# cd /etc
# grep -l root group passwd hosts
group
passwd
To count the number of lines containing the pattern root in the /etc/group file:
# grep -c root group
11
Using regular expression Metacharacters with grep command:
Metachar | Purpose | Example | Result |
^ | Begining of line Anchor | ‘^test’ | Matches all lines begining with test |
$ | End of line anchor | ‘test$’ | Matches all the lines ending with test |
. | Matches one char | ‘t..t’ | Matches all the line starting and ending with t and 2 char between them |
* | Matches the preceding item 0 or more times | ‘[a-s]*’ | Matches all lines starting with lowercase a-s |
[] | Matches one character in the pattern | ‘[Tt]est’ | Matches lines containing test ot Test |
[^] | Matches one character not in pattern | ‘[^a-s]est’ | Matches lines that do not contain “a” though “s” and followed by est |
With egrep we can search one or more files for a pattern using extended regular expression metacharacters.
Metachar
|
Purpose
|
Example
|
Result
|
+ | Matches one of more preceding chars | ‘[a-z]+est’ | Matches one or more lowercase letters followed by est(for example chest, pest, best, test, crest etc |
x|y | Matches either x or y | ‘printer|scanner’ | Matches for either expression |
(|) | Groups characters | ‘(1|2)+’ or ‘test(s|ing)’ | Matches for one or more occurrence. |
Examples:
#egrep ‘[a-z]+day’ /ravi/testdays
sunday
monday
friday
goodday
badday
In the above example, we searched for the letter ending with day in the file /ravi/testdays#egrep ‘(vacation |sick)’ leave’ /ravi/leavedata
vacation leave on 7th march
sick leave on 8th march
In the above example we are displaying sick leave and vacation leave from file /ravi/leavedataUsing fgrep command :
It searches for all the character regardless of it being metacharacter as we have seen in case of grep and egrep commands.
Syntax: fgrep options string filenames
Example:
#fgrep ‘$?*’ /ravi/test
this is for testing fgrep command $?*
#Using Find command :
This command is used to locate files and directories. You can relate it with windows search in terms of functionality.
Syntax: find pathnames expressions actionsPathname: The absolute or relative path from where the search begins.Expressions: The search criteria is mentioned here. We will discuss search criteria below in details.
Expression
|
Definition
|
-name filename | Finds the file matching. |
-size [+|-]n | Finds files that are larger than +n, smaller than -n, or exactly n. |
-atime [+|-]n | Find files that have been accessed more than +n days, less than -n or exactly n days ago. |
-mtime [+|-]n | Find files that have been modified more than +n days, less than -n or exactly n days ago. |
-user loginID | Finds all files that are owned by the loginID name. |
-type | Finds a file type : f for file, d for directory. |
-perm | Find files that have certain access permission bits. |
Action: Action required after all the files have been found. By default it displays all the matching pathnames
Action
|
Definition
|
-exec command {} \; | Runs the specified command on each file located. |
-ok commadn {} \: |
Requires confirmation before the find command applies the command to each file located.
|
Prints the search result | |
-ls | Displays the current pathname and associated stats : inode number, size in kb, protection mode, no. of hard links and the user. |
-user loginID | Finds all files that are owned by the loginID name. |
-type | Finds a file type : f for file, d for directory. |
-perm | Find files that have certain access permission bits. |
Examples:
#touch findtest
#cat >> findtest
This is for test.
#find ~ -name findtest -exec cat {} \;
This is for test.
#
The above examples searches for the file : findtest and displays its content. We can also use ‘ok’ option instead of exec. This will prompt for confirmation before displaying the contents of file findtest.
If we want to find files larger than 10 blocks (1 block = 512bytes) starting from /ravi directory, following command is used :
#find /ravi -size +10
If we want to see all files that have not been modified in the last two days in the directory /ravi, we use :
#find /ravi -mtime +2
Printing Files:
Syntax:
/usr/bin/lp -d <printer name > <file name>
Option
|
Description
|
d | It is used to specify the desired printer. It is not required if default printer is used |
o | It is used to specify that the banner page should not be printed |
n | Print the number of copies specified |
m | It send email after the print job is complete |
Option
|
Description
|
p | Displays the status of all printers |
o | Displays the status of all output printers |
d | Displays the default system printer |
t | Displays the complete status information of all printers |
s | Display the status summary of all printers |
a | Displays which printers are accepting request |
Comments
Tags: Directories And Files, elearning Solaris 10, learning Solaris 10, solaris, Working With Directories And Files in Solaris