0% found this document useful (0 votes)
12 views

os file

Uploaded by

harshkum766
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

os file

Uploaded by

harshkum766
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Basic Commands

1. ECHO COMMAND :-
echo is a built-in command in the bash and C shells that writes its
arguments to standard output.

SYNTAX :-
echo [option(s)] [string(s)]
echo -
EXAMPLE :-

Options of echo commands are :-


a. -n - Do not output the trailing new line
EXAMPLE : -

b. -e - Enable interpretation of the following backslash-escaped


characters in each string
EXAMPLE :-

c. \n - New line
EXAMPLE :-

d. \t - Horizontal tab
EXAMPLE :-

e. \v - Vertical tab

1
EXAMPLE :-

f. \\ - Backslash
EXAMPLE :-

g. To print a variable with the echo we use ‘$’ with variable :-


EXAMPLE :-

h. echo $HOME - used to show the current user home directory


EXAMPLE :-

i. echo $PATH - used to show the path environmental variable


EXAMPLE :-

j. echo command can be used to evaluate the expression

2. DATE :-
Linux “date” command returns you the date and time when you call
it without any options.
Use the date command to display the current date and time or set the
system date / time over ssh session.

SYNTAX :-
date [OPTION]... [+FORMAT]

2
EXAMPLE :-

These are a few of the many possibilities that the “date” command
offers you. Check out “date –help for options”. Some interesting ones
are :-
%D date (mm/dd/yy)
%d day of month (01..31)
%m month (01..12)
%y last two digits of year (00..99)
%a locale’s abbreviated weekday name (Sun..Sat)
%A locale’s full weekday name, variable length (Sunday..Saturday)
%b locale’s abbreviated month name (Jan..Dec)
%B locale’s full month name, variable length (January..December)
%H hour (00..23)
%I hour (01..12)
%Y year (1970…)

a. date +”%d” - to get only date .


EXAMPLE :-

b. date +”%d%m%y” - to get complete date , month , year


EXAMPLE :-

3
c. date +”%a%d%m%y” - to get the day of the week along with
the rest of the date
EXAMPLE :-

d. for formatting use , /


EXAMPLE :-

e. ho
EXAMPLE :-

3. HOSTNAME :-
Hostname is the program that is used to either set or display the
current host, domain or node name of the system. These names are
used by many of the networking programs to identify the machine.

SYNTAX:-
Hostname - to see the host

4
EXAMPLE :-

4. ARCH :-
The arch command will print the type of computer architecture .

EXAMPLE :-

5. UNAME :-
Print na# of the current system .

EXAMPLE :-

6. UPTIME :-
Uptime gunives a one line display of the following information. The
current time, how long the system has been running, how many users
are currently logged on, and the system load averages for the past 1,
5, and 15 minutes.

EXAMPLE :-

5
7. WHOAMI :-
Whoami prints the user name associated with the current effective
user ID.

EXAMPLE :-

8. WHO :-
Displays who is on the system.

SYNTAX :-
who [-H] [-m] [am i] [ file ]
-H Output column headings above the regular output.
-m Output only information about the current terminal.
-u List only those users who are currently logged in.
-am i In the locale, limit the output to describing the invoking user,
equivalent to the -m option. The am and I or I must be separate
arguments.

EXAMPLE :-

6
Directory Commands :-

9. PWD :-
Display name of current directory in the linux system .

EXAMPLE :-

10.CHANGE DIRECTORY(cd) :-
Switch to another directory .

EXAMPLE :-

a. cd - Will pace you in a home directory .


EXAMPLE :-

b. cd / - Will move you to the root directory .

EXAMPLE :-

c. cd / etc - Will move you to the /etc directory .

7
cd /EXAMPLE :-

d. cd .ect./ - Will move you back one directory


EXAMPLE :-

11.LIST(ls) :-
List the Contents of a directory .

SYNTAX :-
ls [option]
cd
EXAMPLE :-

a. ls -l - list all file/directory information in current


directory(long version)
EXAMPLE :-

b. ls -R - list all files in current directories and below


EXAMPLE :-

8
c. ls -It - list all files, sorted by most recent accessed first
EXAMPLE :-

d. ls -It/etc/rc* - list files in the /etc/ directory, only starting with


‘rc’ and sort results by most recent .
EXAMPLE :-

12.CREATE FILES (mkrdir) :-


The command mkdir is used to create a new directory.

SYNTAX :-
mkdir [directory name]

13.REMOVE FILES(rmdir) :-
The command rmdir or rm -r is used to delete a directory or
directories .

SYNTAX :-

9
rmdir [directory name]

EXAMPLE :-

File Commands :-

14.WILDCARDS :-
Wildcard characters are used to help find file or directory names .
Various Wilcards that are used as an option are as follows :-

a. * - asterisk symbol is used to represent any character(s)


EXAMPLE :-
a* all files starting with the letter ‘a’ .

l
*a all files where the last character is ‘a’ .

a*r all files that start with the letter ‘a’ and end with ‘r’ .

b. ? - question mark is used to represent any single character .


EXAMPLE :-
am?? all files that start with ‘am’ and are only four characters
long

10
c. [from-to ] - Values entered within square brackets represent a
range (from-to) for a single character .
EXAMPLE :-
[a -n]* all files that start with ‘a’ , ‘b’ and so on till ‘n’ .

d. [!from-to ] - Values entered within square brackets represent a


range (from-to) to
exclude for a single character .

EXAMPLE :-
[!a-n]* - all files that start with ‘a’ , ‘b’ and so on till ‘n’ are not
be displayed .

15.COPY FILES(cp) :-
To copy a file, the command cp is used .

SYNTAX :-
cp [oldfile] [newfile]

EXAMPLE :-

In the above command , nibha.txt file contain ‘Hello’ which is copied


to amar.txt file

11
16.RENAME FILES(mv) :-
The command mv is used to rename a file .

SYNTAX :-
mv [old name] [new name]

EXAMPLE :-

17.REMOVE(rm) :-
This command removes the file from the directory .

SYNTAX :-
rm filename

EXAMPLE :-

rm -i am* - prompt to remove each file in current directory starting


with ‘am’ .

12
rm am* - remove all files in current directory starting with ‘am’
automatically

18.WORD , LINE COUNT(wc) :-


The command wc is used to count lines, words or characters in a file
or piped results from another command .

SYNTAX :-
wc OPTION... [FILE]...

EXAMPLE :-

13
a. wc -l - This option prints the number of lines present in a file .
Hello is 1 line .

b. wc -w - This option prints the number of words present in a file .


Hello is 1 word .

c. wc -c - This option displays count of bytes present in a file . Hello


is of 6 bytes .

d. wc -m - Using -m option ‘wc’ command displays count of


characters from a file .

19.DISPLAY FILE TYPE(file) :-


Files can consist of several types. The command file is used to display
a description for the type.

SYNTAX :-
file [filename]

EXAMPLE :-

14
20.CONCANTENATE FILES(cat) :-
The command cat is a multi-purpose utility and is mostly used with
TEXT files.
a. Create a new file and optionally allow the manual entry of
contents
EXAMPLES :-

 cat > [filename]


 Example: cat >myfile will create a file named myfile and
allow you to enter contents.
 Press Control-D to exit entry mode.
 WARNING: If “myfile” already existed, this command
would replace the old file with the contents of the new
file.
b. Combine text files - cat file1 file2 > newfile - This will combine
file1 and file2 into newfile.
EXAMPLE :-

c. Display the contents of a file --cat a file


EXAMPLE :-

15
d. Delete the contents of a file --cat /dev/null >a file
EXAMPLE :-

21.TOUCH COMMANDS :-
Touch command is used to create a file .It can change file access and
modification time. It is also used to change the timestamps (i.e., dates
and times of the most recent access and modification)on existing files
and directories.

SYNTAX :-
touch [option] file_name(s)

EXAMPLE :-

Touch’s options :-
a. -a - Change the access time . Do not change the modification
time unless -m is also specified.
EXAMPLE :-

16
b. -c - Do not create a specified file if it does not exist. Do not
write any diagnostic
messages concerning this condition.
EXAMPLE :-

c. -m - Change the modification time of file. Do not change the


access time unless -a is also specified.
EXAMPLE :-

22.PASTE COMMAND :-
Merge corresponding or subsequent lines of files.

SYNTAX :-
paste file1 file 2

17
EXAMPLE :-

23.SORT COMMANDS :-
Sort sorts, merges, or compares all the lines from the given files, or
the standard input if no files are given.

SYNTAX :-
sort [options] [file...]

EXAMPLE :-

Options for sort command :-


a. -f - Fold lower case characters into the equivalent upper case
characters when sorting so that, for EXAMPLE “b” is sorted
the same way “B” is.
EXAMPLE :-

18
b. -r - Reverse the result of comparison, so that lines with greater
key values appear earlier in the output instead of later.
EXAMPLE :-

c. -t - Use character separator as the field separator when finding


the sort keys in each line. By default, fields are separated by the
empty string between a non-whitespace character and a
whitespace character.
EXAMPLE :-

24.CUT COMMANDS :-
The cut command takes a vertical slice of a file, printing only the
specified columns or fields.
Like the sort command, the cut command defines a field as a word
set off by blanks, unless you specify your own delimiter.

19
SYNTAX :-
cut OPTION... [FILE]...

Options for Cut Commands :-


a. -c - The list following -c specifies character positions (for
instance, -c -3 would pass the first 3 characters of each line).
EXAMPLE :-

b. -f - The list following -f is a list of fields assumed to be


separated in the file by a delimiter character .
EXAMPLE :-

c. -d - The character following -d is the field delimiter (-f option


only). Default is tab. Space or other characters with special
meaning to the shell must be specified inside the single quotes
like ‘ ‘ .

20
EXAMPLE

25.HEAD AND TAIL COMMAND :-


The head command displays the first few lines at the top of a file. By
default, head will show the first ten lines of a file, but you can also tell
it how many lines to display.

SYNTAX FOR HEAD :-


head [-number of lines ]filename

EXAMPLE :-
touch

The tail command displays the last few lines of a file. Like head, it
can save you time, because it’s a lot quicker than calling up a file with
a text editor and scrolling all the way down to the bottom.

21
SYNTAX FOR TAIL :-
tail [ number of lines] [file]

EXAMPLE :-

SYNTAX FOR COMBINING HEAD AND TAIL :-


head -n10 filename | tail -n5

EXAMPLE :-

26.SPELL COMMAND :-
Looks through a text file and reports any words that it finds in the
text file that are not in the
dictionary.

SYNTAX :-
spell FILENAME

EXAMPLE :-

22
27.FINGER COMMAND :-
Lists information about the user.

SYNTAX :-
finger [username]

EXAMPLE :-

28.GREP COMMAND :-
grep searches the input files for lines containing a match to a given
pattern list. When it finds a match in a line, it copies the line to
standard output (by default), or whatever other sort of output you
have requested with options.

SYNTAX :-
grep [option] pattern [file_name]
pattern - at what pattern will it be displayed
EXAMPLE :-

23
Various options that helps the grep command to give the suitable
output are as follows :-
a. -i - It prints the matches pattern having both uppercase and
lowercase .
EXAMPLE :-

b. -w - It prints only when the whole word is matched not the


character .
EXAMPLE :-

c. -c - Counts the number of lines that contain matching pattern


in the file and prints it .
EXAMPLE :-

29.CLEAR COMMAND :-
It clears the screen .

SYNTAX :-

24
clear

EXAMPLE :-
BEFORE : -

AFTER :-

30.CHANGE MODE :-
In Unix operating systems, the chmod command is used to change the
access mode of a file. The name is an abbreviation of change mode.
Which states that every file and directory has a set of permissions
that control the permissions like who can read, write or execute the
file.

SYNTAX :- (SYMBOLIC METHOD)


chmod [OPTIONS] [ugoa…][-+=]perms…[,…] FILE...
The first set of flags ([ugoa…]), users flags, defines which users
classes the permissions to the file are changed.
 u - The file owner.
 g - The users who are members of the group.
 o - All other users.
 a - All users, identical to ugo.

25
The second set of flags ([-+=]), the operation flags, defines whether
the permissions are to be removed, added, or set :-
 - Removes the specified permissions.
 + Adds specified permissions.
 = Changes the current permissions to the specified permissions. If no
permissions are specified after the = symbol, all permissions from the
specified user class are removed.

a. chmod g=r filename - Give the members of the group


permission to read the file, but not to write and execute it .
EXAMPLE :-

b. chmod a-x filename - Remove the execute permission for all


users .
EXAMPLE

:-

c. chmod og-rwx filename - Remove the read, write, and execute


permission for all users except the file’s owner .
EXAMPLE :-

26
SYNTAX :- (NUMBERIC METHOD)
chmod [OPTIONS] NUMBER FILE...
Each write, read, and execute permissions have the following number
value:
The NUMBER can be a 3 or 4-digits number. When 3 digits number
is used, the first digit represents the permissions of the file’s owner,
the second one the file’s group, and the last one all other users.
 r (read) = 4
 w (write) = 2
 x (execute) = 1
 no permissions = 0

The permissions number of a specific user class is represented by the


sum of the values of the permissions for that group.
 Owner: rwx=4+2+1=7
 Group: r-x=4+0+1=5
 Others: r-x=4+0+0=4

When the 4 digits number is used, the first digit has the following
meaning:
 setuid=4
 setgid=2
 sticky=1
 no changes = 0

stat command - Check the file’s permissions in the numeric notation


using the stat command:
SYNTAX : -

27
stat -c "%a" filename
EXAMPLE :-

a. chmod 100 dirname - Give the file’s owner read and write
permissions and only read permissions to group members and
all other users .
EXAMPLE :-

b. chmod 750 dirname - Give the file’s owner read, write and
execute permissions, read and execute permissions to group
members and no permissions to all other users .
EXAMPLE :-

MEMORY MANGEMENT COMMANDS:-

31.DISPLAY SYSTEM MEMORY:-


Free command used to check the used and available space of physical
memory and swap
memory in KB. See the command in action below.

SYNTAX:-
# free
EXAMPLE:-

32.DISPLAY MEMORY IN BYTES

28
Free command with option -b, display the size of memory in Bytes.

SYNTAX:-
# free -b

EXAMPLE:-

33.DISPLAY MEMORY IN KILOBYTES:-


Free command with option -k, display the size of memory in
(KB) Kilobytes.

SYNTAX:-
# free -k

EXAMPLE:-

34.DISPLAY MEMORY IN MEGABYTES:-


To see the size of the memory in (MB) Megabytes use option as -m.

SYNTAX:-
#free -m

EXAMPLE:-

29
35.DISPLAY MEMORY IN GIGABYTES:-
Using -g option with free command, would display the size of the
memory in GB(Gigabytes).

SYNTAX:-
#free -g

EXAMPLE:-

36.DISPLAY TOTAL LINE:-


Free command with -t option, will list the total line at the end.

SYNTAX:-
#free -t

EXAMPLE:-

37.DISABLE DISPLAY OF BUFFER ADJUSTED LINE:-


By default the free command display “buffer adjusted” line, to
disable this line use option as -o.

30
SYNTAX:-
#free -o

EXAMPLE:-

38.DISPLAY MEMORY STATUS FOR REGULAR INTERVALS:-


The -s option with number, used to update free command at regular
intervals. For example, the
below command will update free command every 5 seconds.

SYNTAX:-
# free -s 5

EXAMPLE:-

39.SHOW LOW AND HIGH MEMORY STATISTICS:-


The -l switch displays detailed high and low memory size statistics.

SYNTAX:-
# free -l

EXAMPLE:-

31
40.CHECK FREE VERSIONS:-
The -V option, display free command version information.

SYNTAX:-
# free -V

EXAMPLE:-

32
33

You might also like