Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Shell Scripts

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Shell Scripts

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Shell Scripts

Write a shell script to find complete path of any file

clear

echo Pl. input file name

read filename

if test -e $filename

then

find . -name $filename -print

else

echo $filename does not exists

fi

Write a shell script to execute


following commands
1. Sort file abc.txt and save this sorted file in txt
2. Give an example of : To execute commands together without affecting result of each other.
3. How to print three line Text message using one echo command
4. Which command display version of the UNIX?
5. How would u get online help of cat command?

Ans.

echo “sorting the file”

sort abc.txt > xyz.txt

echo “executing two commands”

who ; ls

echo –e “this is \n a three-line \n Text message”

echo “The version is `uname -a`”

echo “Help of cat command”

man cat

Write a shell script to execute


following commands
1. How would u display the hidden files?
2. How delete directory with files?
3. How would user can do interactive copying?
4. How would user can do interactive deletion of files?
5. Explain two functionality of “mv” command with example?

Ans.

clear

echo “1. How would u display the hidden files”

echo “2. How delete directory with files”

echo “3. How would user can do interactive copying”

echo “4. How would user can do interactive deletion of files”

echo “5. Explain two functionality of “mv” command with example” echo “enter your choice”

read ch

case $ch in

1)echo “Displaying hidden files”

ls .[a-z]* ;;

2)echo “Deleting directories with files”

rm -R dirname

3)echo “Interactive copy”

cp -i file1 file2 ;;

# file2 should be created first to check interactivity

4)echo “Interactive Deletion”

rm -i file1 ;;

5)echo “mv command”

mv oldfilename newfilename ;;

*) echo “Invalid choice” ;;

esac

Write a shell script to execute


following commands
1. Create a file called text and store name, age and address in
2. Display the contents of the file text on the screen.
3. Delete the directories mydir and newdir at once
4. Sort a numeric file?
5. Change the permissions for the file newtext to

Ans.
clear

echo “1. Create a file called text and store name,age and address in it.”

echo “2. Display the contents of the file text on the screen.”

echo “3. Delete the directories mydir and newdir at one shot.”

echo “4. Sort a numeric file”

echo “5. Change the permissions for the file newtext to 666.”

echo “enter your choice”

read ch

case $ch in

1)echo “Create a file called text and store name,age and address in ”

echo “Enter the filename”

read fn

cat > $fn ;;

2)echo “Display the contents of the file text on the ”

cat $fn ;;

3)echo “Delete the directories mydir and newdir at one ”

rmdir mydir newdir ;;

4)echo “Sort a numeric file”

sort -n filename ;;

5)echo “Change the permissions for the file newtext to ”

chmod 666 newtext ;;

*) echo “Invalid choice” ;;

esac

Write shell script that accept


filename and displays last
modification time if file exists,
otherwise display appropriate
message.
Ans.

stat -c “%y” filename


OR

if [ -e $fn ]

then

ls -l $fn | cut -d “ “ -f8

#change the column number for desired output

else

echo “File does not exist”

fi

Write a shell script to display the


login names that begin with ‘s’.
Ans.

who | grep ^s

Write a shell script to remove the


zero sized file from the current
directory
Ans.

for i in *

do

if [ ! -s $i ]

then

rm $i

echo ” $i removed ”

fi

done

OR

$find . -size 0 –delete

Write a shell script to display the


name of all the executable file
from the current directory.
Ans.

for i in *

do

if [ -x $i ] then

countx=`expr $countx + 1`

echo $i

fi

echo “Number of executable files are $countx”

Write a shell script that will display


welcome message according to
time
Ans.

d=`date +”%H”`

if [ $d -lt 12 ]

then

echo “Good Morning”

elif [ $d -gt 12 -a $d -lt 14 ]

then

echo “Good Afternoon”

else

echo “Good Evening”

fi

Write a shell script to find number


of ordinary files and directory files.
Ans.

for i in *
do

if [ -d $i ]

then

countd=`expr $countd + 1`

fi

if [ -f $i ]

then

countf=`expr $countf + 1`

fi

done

echo “Number of directories are $countd ”

echo “Number of Ordinary files are $countf”

Write a shell script that takes a


filename from the command line
and checks whether the file is an
ordinary file or not.
If it is an ordinary file then it should display the contents of the

If it is not an ordinary file then script should display the message: “ File does not exist or is not
ordinary, cannot display. “
Ans.

if [ -f $1 ]

then

echo “It is an ordinary file”

cat $1

else

echo “File does not exist or is not ordinary file”

fi

Write a shell script that takes a


filename from the user and checks
whether it is a directory file or not.
If it is a directory, then the script should display the contents of the

If it is not a directory file then script should display the message: “File is not a directory file"

Ans:

echo “enter the filename”

read fn

if [ -d $fn ]

then

echo “Its a directory”

ls $fn

else

echo “Its not a directory”

fi

Write a shell script that takes a


filename as an argument and
checks if the file exists and is
executable.
If the file is executable then the shell script should display the message: “File exists”

If the file does not exists and is not executable then the script should display the message: “File does not exist or is not

Ans.
echo “enter the filename”

read fn

if [ -e $fn -a -x $fn ]

then

echo “file exists and is executable”


else

echo “file does not exist or is not executable”

fi

Write a shell script that displays


all subdirectories in current
working directory.
Ans.

echo “List of Directories. ”

for i in *

do

if [ -d $i ]

then

echo $i

fi

done

OR

find . –type d –print

Write a shell script that calculates


the number of ordinary and
directory files in your current
working directory.
Ans.

for i in *

do

if [ -d $i ]

then

countd=`expr $countd + 1`
fi

if [ -f $i ]

then

countf=`expr $countf + 1`

fi

done

echo “Number of directories are $countd ”

echo “Number of Ordinary files are $countf”

Write a shell script that accepts 2


filenames and checks if both
exists; if both exist then append
the content of the second file into
the first file.
Ans.

echo “enter the first filename”

read fn1

echo “enter the second filename”

read fn2

if [ -f $fn1 -a -f $fn2 ]

then

echo “Both file exists” cat $fn2 >> $fn1

else

echo “Files does not exist”

fi

Write a shell script that takes the


name of two files as arguments
and performs the following:
1. Displays the message :“Displaying the contents of file first argument)”and displays the contents page
wise.

2.Copies the contents of the first argument to second

3.Finally displays the message : “File copied successfully.”

Ans.

echo “Displaying the contents of file $1”

cat $1

echo “Displaying the contents page wise”

cat $1 | more

echo “Copying the files”

cp $1 $2

c=`echo $?`

if [ $c -eq 0 ]

then

echo “File copied successfully”

else

echo “Files not copied successfully”

fi

Write a shell script to display the


following menu and acts
accordingly:
1. Calendar of the current month
2. Display “Good Morning/Good Afternoon/Good Evening” according to the current login time.
3. display User name, Users home directory
4. Terminal name, Terminal type.
5. Machine name
6. No. of users who are currently logged in; List of users who are currently logged in.

Ans:

clear
echo “Menu”

echo “1. Calendar of the current month and year.”

echo “2. Display “Good Morning/Good Afternoon/Good Evening” according to the current login time.”

echo “3. User name, Users home directory.” echo “4. Terminal name, Terminal type.”

echo “5 Machine name.”

echo “6. No. of users who are currently logged in; List of users who are currently logged in.”

echo “enter your choice”

read ch

case $ch in

1) echo “Calendar of current month is”

cal ;;

2) d=`date +”%H”`

if [ $d -lt 12 ]

then

echo “Good Morning”

elif [ $d -gt 12 -a $d -lt 16 ]

then

echo “Good Afternoon”

else

echo “Good Evening”

fi

2)echo “Username is $USER”

3)echo “Users Home directory is $HOME” ;;

4)echo “Terminal details” tty;;

5) echo “Machine name is” uname -m ;;

6)echo “The number of users logged in are” who | wc -l

*) echo “Invalid choice”

esac

Write a shell script that displays


the following menu and acts
accordingly
1. Concatenates two strings
2. Renames a file
3. Delete a file
4. Copy the file to specific location

Ans.

echo “1. Concatenates two strings ”

echo “2. Renames a file”

echo “3. Deletes a file.”

echo “4. Copy the file to specific location” echo “enter your choice”

read ch

case $ch in

1)echo “enter first string”

read str1

echo “enter second string”

read str2

echo “The concated strings are $str1$str2” ;;

2) echo “enter the old filename”

read ofn

echo “enter the new filename”

read nfn

mv $ofn $nfn

echo “file renamed” ;;

3) echo “enter the filename”

read fn

rm $fn

echo “file deleted” ;;

4)echo “enter the filename”

read fn

cp $fn \usr\home\dir\$fn

#you can change the specific path echo “file copied” ;;

*) echo “invalid choice” ;;

esac
Write a shell script to change the
suffix of all your *.txt files to .dat.
Ans.

suffix=”.dat”
for i in `ls *.txt`
do
name=`basename $i .txt`
mv $i $name$suffix

done

Write a shell script to accept a


directory-name and display its
contents. If input is not given then
HOME directory’s contents should
be listed. (Make use of command
line argument)
Ans.

if [ $# ]

then

ls $1

else

ls $HOME

fi

To execute do as below

$sh filename.sh dir1


Write a shell script to get all files
of home directory and rename
them if their names start with c.
e.g Newname = oldname111
Ans.

suffix=111
for f in `ls *`
do
echo filename $f
extension=${f##*.}
echo $extension
name=`basename $f .$extension`
if test -z $extension
then
mv $f $f$suffix
else
mv $f $name$suffix”.”$extension
fi
done

Write a shell script that takes two


filename as arguments. It should
check whether the contents of two
files are same or not, if they are
same then second file should be
deleted.
Ans.

echo “enter the first filename”

read fn1

echo “enter the second filename”

read fn2
cmp $fn1 $fn2

c=`echo $?`

if [ $c -eq 0 ]

then

echo “both files are same”

rm $fn2

else

echo “both files are not same”

fi

$sh fn.sh dir1 dir2

Write a shell script that accepts


two directory names from the
command line and copies all the
files of one directory to another.
The script should do the following

If the source directory does not exist, flash an error message

If destination directory does not exist create it

Once both exist copy all the files from source directory to destination directory.

Ans.

if [ $# ]

then

if [ -d $1 ]

then

if [ -d $2 ]

then

cp -R $1 $2 else

mkdir $2

echo “Directory created $2” cp -R $1 $2

fi

else
echo “source directory does not exist”

fi

else

echo “Please provide command line arguments”

fi

Write a shell script that displays


the following menu
List home directory

• Date

Print working directory

• Users logged in

Read the proper choice. Execute corresponding command. Check for invalid choice.

Ans.
echo “1.List home directory”

echo “2.Date”

echo “3. Print working directory”

echo “4. Users logged in”

echo “enter your choice”

read ch

case $ch in

1)echo “Home directory is $HOME” ;;

2)echo “Todays date is `date` ” ;;

3)echo “Present working directory is `pwd` ” ;;

4)echo “ No of users logged in are” who ;;

*) echo “Invalid choice” ;;

esac

Write a shell script that displays all


hidden files in current directory.
Ans. $ ls -a | egrep ‘^\.’

OR

ls .[a-z]*

Write a shell script that Combine


two files in the third file
horizontally and vertically. Ans.
echo “enter the first filename”

read fn1

echo “enter the second filename”

read fn2

echo “Combining two files horizontally”

cat $fn2 >> $fn1

echo “Combining two files vertically”

paste $fn1 $fn2

Write a shell script to delete all the


spaces from a given file.
Ans.

echo “enter the filename”

read datafile

cat $datafile | tr -d ‘[:space:]’ > newfile

Write a shell script to find a given


date fall on a weekday or a
weekend.
Ans.

d=`date +%u`

if [ $d –eq 7]

then

echo “it is weekend”

else

echo “it is a weekday”

fi

Write a shell script to search for a


given word in all the files given as
the arguments on the command
line.
Ans.

echo “Enter the word”

read w

for i in $@

do

grep $w $i

done

Write a shell script that display last


modified file in the current
directory.
Ans. ls -lt | head -2 | tail -1
Write a script to display the
permissions of the particular file.
Ans.

echo “enter the filename”

read fn

if [ -r $fn ]

then

echo “Read permission”

fi

if [ -x $fn ]

then

echo “eXecute permission”

fi

if [ -w $fn ]

then

echo “Write permission”

fi

OR

ls -l $fn | cut -c 2-10

Write a shell script to


display the calendar in
the following manner:
1. Display the calendar of months m1 and m2 by ‘CAL m1, m2’ command

ii. Display the calendar of the months from m1 to m2 by ‘CAL m1-m2’ command file.

Ans.
m1=$1

y1=$2

wat=$3

m2=$4

y2=$5

if test $wat = “,”

then

cal $m1 $y1;

cal $m2 $y2

elif test $wat = “-”

then

while [ $y1 -le $y2 ]

do

cal $m1 $y1

read a

if [ $y1 -eq $y2 -a $m1 -ge $m2 ]

then

exit

fi

m1=`expr $m1 + 1`

if [ $m1 -gt 12 ]

then

m1=1

y1=`expr $y1 + 1`

done

else

echo Syntax Mth1 Yr1 [,/-] Mth2 Yr2

fi

fi

# run the shell script as

sh -x calrange.sh 1 2019 , 11 2019

sh -x calrange.sh 1 2019 – 11 2019

sh -x calrange.sh 1 2019 # 11 2019


Write a shell script to display the
following menu for a particular file
:
Display all the words of a file in ascending

Display a file in descending order.

Toggle all the characters in the

Display type of the file.

Ans.

echo “1. Display all the words of a file in ascending order.”

echo “2. Display a file in descending order.”

echo “3. Toggle all the characters in the file.”

echo “4. Display type of the file.”

echo “enter your choice” read ch

echo “enter the filename” read fn

case $ch in

sort $fn;;
sort -r $fn;;

3) cat $fn | tr “[a-z][A-Z]” “[A-Z][a-z]”

4) file $fn;;

*) echo “invalid choice”

esac

Write a shell script to check


whether the named user is
currently logged in or not.
Ans. echo “enter the username”
read un

c=`who | grep -c $un`

if [ $c -gt 0 ]

then

echo “User is currently logged in ”

else

echo “User is not currently logged in”

fi

Write a shell script to display the


following menu for a particular file:
Display all the words of a file in ascending

Display a file in descending order.

Display a file in reerse

Toggle all the characters in the file

Display type of the

Ans. echo “1.Display all the words of a file in ascending order.”

echo “2.Display a file in descending order.”

echo “3.Display a file in reerse order.”

echo “4.Toggle all the characters in the file”

echo “5.Display type of the file.”

echo “Enter your choice”

read ch

echo “enter the file name”

read fn

case $ch in

1)sort $fn ;;

2)sort -r $fn ;;

3)rev $fn

4) cat $fn | tr “[a-z][A-Z]” “[A-Z][a-z]”

5) file $fn
*) echo “Invalid choice”

esac

Write a shell script to find total no.


Of users and finds out how many
of them are currently logged in.
Ans. echo “The number of users in the system are”

cat etc/passwd | wc -l

echo “The number of uses currently logged in are “ who | wc –l

OR

total=`cat /etc/passwd | wc -l` cur_log=`who | wc -l`

echo “Total users : $total”

echo “Currently logged : $cur_log”

Write a shell script that displays


the directory information in the
following format- Filename Size
Date Protection Owner
Ans.

x=`ls -l | wc -l`

i=2

echo “Filename\t\t\tSize\tDate\tProtection\tOwner\n”

while [ $i -le $x ]

do

s=`ls -1 | head -$i | tail -l | tr -s ” “`

fn=`echo $s | cut -d ” ” -f 9`

si=`echo $s | cut -d ” ” -f 5`
d1=`echo $s | cut -d ” ” -f 6`

d2=`echo $s | cut -d ” ” -f 7`

p=`echo $s | cut -d ” ” -f 1`

o=`echo $s | cut -d ” ” -f 3`

echo “$fn\t\t\t$si\t$d1 $d2\t$p\t$o” i=`expr $i + 1`

done

OR

echo “Enter the filename” read fn

echo “ Filename Size Date Protection Owner”

echo “`ls -l $fn | cut -d ‘ ‘ -f3` ` ls -l $fn | cut -d ‘ ‘ -f5` `ls -l $fn | cut -d ‘ ‘ -f6,7`
`ls -l $fn | cut -d ‘ ‘ -f1` `ls -l $fn | cut -d ‘ ‘ -f4` “

Write a shell script to display five


largest files from the current
directory

Ans.

ls -lS | head -6 | tail -1

Write a shell script that toggles


contents of the file
Ans.

echo “Enter the filename”

read fn

cat $fn | tr “[a-z][A-Z]” “[A-Z][a-z]”

Write a shell script that report


whether your friend has currently
logged in or not.
Ans.

echo “Enter the username”

read un

c=`who | grep -c $un`

if [ $c -gt 0 ]

then

echo “User is currently logged in ”

else

echo “User is not currently logged in”

fi

Write a shell script to accept any


character using command line and
list all the files starting with that
character in the current directory
Ans.

ls | grep ^$1

# run the shell script as: sh filename.sh a # it will list files starting with a

Create a file called student


containing roll-no, name and
marks.
Display the contents of the file sorted by marks in descending order

Display the names of students in alphabetical order ignoring the case.


Display students according to their roll

Sort file according to the second field and save it to file ‘names’.

Display the list of students who scored between 70 and 80

Ans.

echo “enter the filename”

read fn

cat > $fn

# enter roll-no | name | marks of students and press ctlr+d

echo “1. Display the contents of the file sorted by marks in descending order”

echo “2. Display the names of students in alphabetical order ignoring the case.”

echo “3. Display students according to their roll nos.”

echo “4. Sort file according to the second field and save it to file ‘names’.”

echo “5. Display the list of students who scored between 70 and 80”

echo “enter your choice”

read ch

case $ch in

1)sort -k5 -r $fn ;;

2)sort -k3 -i $fn ;;

3)sort $fn ;;

4)sort -k3 $fn > names ;;

5) awk ‘{ if( $5 > 70 && $5 < 80 ) print $5 }’ $fn ;;

*) echo “Invalid Choice”

esac

File Management Commands


Linux uses some conventions for present and parent directories. This can be a little confusing for beginners. Whenever
you are in a terminal in Linux, you will be in what is called the current working directory. Often your command prompt
will display either the full working directory, or just the last part of that directory. Your prompt could look like one of the
following:

user@dotcom ~/somedir $ user@ dotcom somedir $

user@ dotcom /home/user/somedir $

which says that your current working directory is /home/user/somedir.


In Linux .. represents the parent
directory and . represents the
current directory.
Therefore, if the current directory is /home/user/somedir, then cd ../somedir will not change the working directory.

The table below lists some of the most used file management commands File/directory permissions and groups

Command Utility

GoalKicker.com – Linux® Notes for Professionals 5

chmod <specification> filename Change the file permissions. Specifications = u user, g group, o other, + add
permission, – remove, r read, w write,x execute.

chmod -R <specification> dirname

Change the permissions of a directory recursively. To change permission of a directory and everything within that
directory, use this command.

chmod go=+r myfile Add read permission for the owner and the group. chmod a +rwx myfile Allow all users to read,
write or execute myfile. chmod go -r myfile Remove read permission from the group and others. chown owner1 filename
Change ownership of a file to user owner1.

chgrp grp_owner filename Change primary group ownership of file filename to group grp_owner.

chgrp -R grp_owner dir-name

Change primary group ownership of directory dir-name to group grp_owner recursively. To change group ownership of a
directory and everything within that directory, use this command.

Section 1.3: Hello World

Type the following code into your terminal, then press Enter :

echo “Hello World”


This will produce the following output:

Hello World

Section 1.4: Basic Linux Utilities

Linux has a command for almost any tasks and most of them are intuitive and easily interpreted. Getting Help in Linux

Command Usability

man <name> Read the manual page of <name>.

man <section> <name> Read the manual page of <name>, related to the given section. man -k <editor> Output all the
software whose man pages contain <editor> keyword. man -K <keyword> Outputs all man pages containing <keyword>
within them.

apropos <editor> Output all the applications whose one line description matches the word ditor. When not able to recall
the name of the application, use this command.

help In Bash shell, this will display the list of all available bash commands.

help <name> In Bash shell, this will display the info about the <name> bash command. info <name> View all the
information about <name>.

dpkg -l Output a list of all installed packages on a Debian-based system.

dpkg -L packageName Will list out the files installed and path details for a given package on Debian.

dpkg -l | grep -i <edit> Return all .deb installed packages with <edit> irrespective of cases. less /var/lib/dpkg/available
Return descriptions of all available packages.

whatis vim List a one-line description of vim.

<command-name> –help Display usage information about the <tool-name>. Sometimes command -h also works, but not
for all commands.

User identification and who is who in Linux world Command Usability hostname Display hostname of the system.

GoalKicker.com – Linux® Notes for Professionals 6

hostname -f Displays Fully Qualified Domain Name (FQDN) of the system. passwd Change password of current user.

whoami Username of the users logged in at the terminal. who List of all the users currently logged in as a user.
w Display current system status, time, duration, list of users currently logged in on system and other user information.

last Who recently used the system.

last root When was the last time root logged in as user. lastb Shows all bad login attempts into the system.

chmod Changing permissions – read,write,execute of a file or directory.

Process related information Command Usability

top List all processes sorted by their current system resource usage. Displays a continually updated display of
processes (By default 3 seconds). Use q key to exit top.

ps List processes currently running on current shell session

ps -u root List all of the processes and commands root is running ps aux List all the processes by all users on the
current system

Section 1.5: Searching for files by patterns in name/contents

A common and task of someone using the Linux Command Line (shell) is to search for files/directories with a certain
name or containing certain text. There are 2 commands you should familiarise yourself with in order to accomplish this:

Find files by name

find /var/www -name ‘*.css’

This will print out the full path/filename to all files under /var/www that end in .css. Example output:

/var/www/html/text-cursor.css

/var/www/html/style.css

For more info:

man find

Find files containing text

grep font /var/www/html/style.css

This will print all lines containing the pattern font in the specified file. Example output: font-weight: bold;

font-family: monospace; Another example:

grep font /var/www/html/

GoalKicker.com – Linux® Notes for Professionals 7 This doesn’t work as you’d hoped. You get:

grep: /var/www/html/: Is a directory


You need to grep recursively to make it work, using the -R option:

grep -R font /var/www/html/

Hey nice! Check out the output of this one:

/var/www/html/admin/index.php: echo ‘<font color=red><b>Error: no dice</b></font><br/>’;

/var/www/html/admin/index.php: echo ‘<font color=red><b>Error: try again</b></font><br/>’;

/var/www/html/style.css: font-weight: bold;

/var/www/html/style.css: font-family: monospace;

Notice that when grep is matching multiple files, it prefixes the matched lines with the filenames. You can use the –

h option to get rid of that, if you want.

For more info:

man grep

Section 1.6: File Manipulation

Files and directories (another name for folders) are at the heart of Linux, so being able to create, view, move, and
delete them from the command line is very important and quite powerful. These file manipulation commands allow you
to perform the same tasks that a graphical file explorer would perform.

Create an empty text file called myFile:

touch myFile

Rename myFile to myFirstFile:

mv myFile myFirstFile View the contents of a file:

cat myFirstFile

View the content of a file with pager (one screenful at a time): less myFirstFile

View the first several lines of a file:

head myFirstFile

View the last several lines of a file:

tail myFirstFile Edit a file:

GoalKicker.com – Linux® Notes for Professionals 8 vi myFirstFile

See what files are in your current working directory:

ls

Create an empty directory called myFirstDirectory:

mkdir myFirstDirectory

Create multi path directory: (creates two directories, src and myFirstDirectory) mkdir -p src/myFirstDirectory
Move the file into the directory:

mv myFirstFile myFirstDirectory/ You can also rename the file:

user@linux-computer:~$ mv myFirstFile secondFileName Change the current working directory to myFirstDirectory:

cd myFirstDirectory Delete a file:

rm myFirstFile

Move into the parent directory (which is represented as ..):

cd ..

Delete an empty directory:

rmdir myFirstDirectory

Delete a non-empty directory (i.e. contains files and/or other directories): rm -rf myFirstDirectory

Make note that when deleting directories, that you delete ./ not / that will wipe your whole filesystem.

Section 1.7: File/Directory details

The ls command has several options that can be used together to show more information. Details/Rights

The l option shows the file permissions, size, and last modified date. So if the root directory contained a dir called test
and a file someFile the command:

GoalKicker.com – Linux® Notes for Professionals 9 user@linux-computer:~$ ls -l

Would output something like

-rw-r–r– 1 user users 70 Jul 22 13:36 someFile.txt

drwxrwxrwx 2 user users 4096 Jul 21 07:18 test

The permissions are in format of drwxrwxrwx. The first character represents the file type d if it’s a directory -otherwise.
The next three rwx are the permissions the user has over the file, the next three are the permissions the group has over
the file, and the last three are the permissions everyone else has over the file.

The r of rwx stands for if a file can be read, the w represents if the file can be modified, and the x stands for if the file
can be executed. If any permission isn’t granted a – will be in place of r, w, or x.

So from above user can read and modify someFile.txt but the group has only read-only rights.

To change rights you can use the chmod ### fileName command if you have sudo rights. r is represented by a value of
4, w is represented by 2, and x is represented by a 1. So if only you want to be able to modify the contents to the test
directory

Owner rwx = 4+2+1 = 7

Group r-x = 4+0+1 = 5

Other r-x = 4+0+1 = 5

So the whole command is chmod 755 test


Now doing a ls -l would show something like drwxr-xr-x 2 user users 4096 Jul 21 07:20 test Readable Size

Used in conjunction with the l option the h option shows file sizes that are human readable.

Running

user@linux-computer:~$ ls -lh Would output:

total 4166

-rw-r–r– 1 user users 70 Jul 22 13:36 someFile.txt

drwxrwxrwx 2 user users 4.0K Jul 21 07:18 test Hidden

To view hidden files use the a option. For example user@linux-computer:~$ ls -a

Might list

GoalKicker.com – Linux® Notes for Professionals 10

.profile someFile.txt test

Total Directory Size

To view the size of the current directory use the s option (the h option can also be used to make the size more

readable).

user@linux-computer:~$ ls -s Outputs

total 4166 someFile.txt test Recursive View

You might also like