Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

List of Commands in Linux

YASH PAL, 3 June 202328 May 2024

chmod command

Changing access permission for files and directories.

chmod changes the permissions of the given file according to mode, which can be either a symbolic representation of changes to make or an octal number representing the b pattern for the new permissions. It manages the mode of the file.

chmod has two modes:

Symbolic mode

  • u,g, or a to represent owner, group, and other
  • + or – to add or remove permission.
  • r, w and x to represent read, write and execute

Numeric mode.

  • 0 represents —
  • 1 represents –x
  • 2 represents -w-
  • 3 represents -wx
  • 4 represents r–
  • 5 represents r-x
  • 6 represents rw-
  • 7 represents rwx

In both cases the chmod [ symbolic or absolute permission) [ file or directory is used).

$chmod 777 sample1
$ls -l

Answer is -rwxrwx

If we change permission using this way then it is referred to as absolute mode. Another way of changing permissions of syntax chmod is referred to as a symbolic mode

$chmode [who] [+/-/= ] [ permission] file

Here who means owner (u), group (g), or other (o). If none is specified then by default all are assumed.

Next + refer to add permission, – refer to remove permission, and = refer to add the specified permission and take away all other permissions
$chmod +r sample1

This command gives read permission to all.
$chmod -r sample1

This command takes away read permission from all.
$ls -l
–wx -wx-wx
$chmod go – w sample1

In order to take away written permission from the group as well as others.

$chmod go – w, go +r sample1

Here take away written permission and give read permission to the group and others.

$chmod go=w, u = rwx sample1

This removes existing permission and replaces them with written permission for the group and others while reading, writing, and executing permission for the owner of file sample 1.

When using chmod with numeric arguments, the values for each granted access right have to be counted together per group. Thus we get a 3 – digit number, which is the symbolic value for the setting chmod has to make. The following table lists the most common combinations:

File protection with chmod

CommandMeaning
chmod 400 fileTo protect a file against accidental overwriting.
chmod 500 directoryTo protect yourself from accidentally removing, renaming, or moving files from this directory.
chmod 600 fileA private file is only changeable by the user who entered this command.
chmod 644 fileA publicly readable file that can only be changed by the issuing user.
chmod 660 fileUsers belonging to your group can change this files, others don’t have any access to it at all.
chmod 700 fileProtects a file against any access from other users, while the issuing user still has full access.
chmod 755 directoryFor files that should be readable and executable by others, but only changeable by the issuing user.
chmod 755 fileStandard file sharing mode for a group.
chmod 777 fileEverybody can do everything to this file.

If you enter a number with less than three digits as an argument to chmod, omitted characters are replaced with zeros starting from the left. There is actually a fourth digit on Linux systems, that precedes the first three and sets special access modes. Everything about these and many more are located on the Info pages.

chown command

Changing file owner
chown transfer ownership of a file to a user. Changing ownership requires superuser permission so first, we have to login into admin mode by su command.

su command for the superuser.
$su
password ****
[ This is the root password ]
→ # It is used by root
#ls -1 file1
-rwxr—x 1 kumar group 333 jun 10:11:12 file1
#chown sharma file1
#ls -1 file1l
-rwxr — x 1 sharma group 333 jun 10:11:12 file1

Once ownership of the file has been given to Sharma therefore Kumar can no longer edit the file file1.

chgrp command

Changing group owner – By default, the group owner of a file is the group to which the owner belongs The chgrp command changes a file’s group owner.

In this command, no superuser’s permission is required.
$chgrp dba file1
$ls -1 file1
-rwxr — x 1 kumar dba 333 jun 10:11:12 file1

A user can change the group owner of a file but only to a group to which he also belongs If Kumar is also a member of the dba group. If he is not then only the superuser can make the command work.

chown command is used to change both owner and group but Linux allows the administrator to use only chown.
#chown Sharma: dba file1
ownership to Sharma, group to dba

date command

$date
Thursday, Sep 17 08:20:10 IST 2022

To display the current date and time. IST signifies Indian Standard Time
The output of data can be modified by a variety of switches.
$date ‘+ DATE: % d | % m | % y % n TIME: %H|%M|% S’

If we want to print the data must be enclosed within a pair of single quotes and the format should begin with a # sign. In which %d, %m and %y signify day, month, and year whereas H. M and % S signify hour, minute, and second. The % n ensures that what follows is displayed on a new line.

Output is:
DATE: 17/09/09
TIME: 08/20/10

cal for calendar command

cal command is capable of printing calendar for any year in the range 1 to 9999.

$cal
Thu Sep 17 08:25:05 2022

The output shows the current date as well as the calendar of the preceding, current, and succeeding months. If we want to see calendar for only a particular month. For example September 2022 then:

$cal 9 2022

The 9 indicates September, in place of 9 we used sep, or ‘s‘ but we can’t use ‘cal j 2022‘ to obtain January 2022’s calendar. Because’J’started June or July. If we become more specific and say ‘cal ja 2022‘, it works but no. of the shell is not working so try to avoid using ‘cal ja 2006‘ instead of that we can use ‘cal 1 2022′.

$cal
Gives calendar of the current month This command gives the calender from year 1 to 9999.

  • $cal 9 09 -> It gives calendar of September 0009″.
  • $cal 9  -> It gives calendar of 9 years.
  • $cal 1  -> It gives calendar of 0001 years.
  • $cal 9 2022  -> It gives calendar for September 2022.

man command

It provides online help about every single command with all the options that the command can support.

To invoke the online manual just type the command man along with the command we wish to seek help. For example:
$man command name
$man cal

It gives the help manual of the command”Cal”. The manual page that is displayed or the source is identical to the manual supplied with the operating system.

  • Name: Describes the name of the command followed by a brief description of that command.
  • Syntax: How the command is used.
  • Square bracket [ ]: Indicates optional arguments.
  • Description: Describes the command.

cat command

It is used to create files.
$cat > sample1

[br]
Now press the enter key and we would find the cursor positioned in the next line type the matter that we want to store in the file sample1 after that press the key ctrl d. ctrl d indicates the end of the file then enter and it saves the matter on the disk in the file sample1. After that, we get back the prompt on the screen type the following command.

$cat sample1
To display the content of a file under DOS that saves in computer/file system/root/sample1).

To see the contents of the file sample1 that we created above we can use another command.

$cat <sample1
cat also concatenates the content of two files and stores them in the third file.

$cat file1 file2 > newfile

If newfile is already something it would be overwritten. If we want that it should remain intact and the content of file1 and file2 should get appended to it then we use append of p redirection operator.

$cat file1 file2 >> newfile

cp command

It is used to copy the content of the file. $cp file1 file2 This command copies the content of file1 to file2. If file2 does not exist then it will be created. If file2 already exists then the cp command overwrites the file2. cp command is used to copying the content of the file to file2.

$cp – file1 file 2

With the option “-i” if the file “file2” exist then it will be prompted before it is overwritten Like if we want to copy file1 to file2 if we press y then the command is executed.

mv command

Renaming of files in DOS is interpreted in Unix as moving them. Suppose we want to rename the file file1 to file2.

$mv file1 file 2
mv command also has the power to rename directories.

[br]
$mv oldfiledir newfiledir
In which oldfiledir will be renamed to newfiledir. my command is removing it from its current location and copying it at a new location.

$mv filel file2 newfiledir

On execution of this command file1 and file2 are no longer present at their original location but are moved to the directory newfiledir.

wc command

The wc utility displays the number of lines, words, and characters in the specified file or files. It comes with the option -l, -w, and -c. Which allows the user to obtain the number of lines, words, and characters. If we specify more than one file on the command line, wc displays the total for each file and totals for the group of files.

Suppose sample 1
Hi I
am

sample 2
How
are
you

$wc -lc sample1
2   7

$we -lwc sample sample2
2 3 7 sample1
3 3 12 sample2
5 6 19 Total

The line at the bottom shows the sum of each column.

$wc press enter

This command is capable of accepting input directly from the keyboard after entering some input and using ctrl d. The appropriate count is displayed for the input that user-supplied.

who command

who command displays data about all the users who have logged into the system currently.

$who
root tty3a 2022-09-24 09:20
root1tty3c 2022-09-25 09:25

$who am i

root tty3a 2022-09-24 09:20

In which root is a login name,”tty3a” show that the terminal number is the serial port line by which our terminal is connected to the host machine, and after that 2022-09-24 is the date 09:20 is the time at which we logged.

ls command

Gives the directory listing or lists the contents of the current or specified directory. It displays information about one or more files. By default, it displays a short listing containing only the name of the file.

$ls

file1 file2 file3 gr nom

If we create a file name beginning with ‘.’ then no error has occurred.

$cat > file4

It contains the file1
file2 file3
ctrl d

$ls
filel file2 file3 gr nom

What about file4 if any filename beginning with a ‘.’ is treated as a hidden file? If we want to list hidden files we need to use the -a option of ls.

$ls – a file4 filel file2 file3 gr nom

‘..’ stand for the parent of the current directory while standing for the current directory ls -a list all the files including the hidden file.

Metacharacters

Metacharacters are representative of one or a group of characters.

$ls p *
It displays all the files whose names begin with p. * interpreted by the shell presence of any number of characters.

$ls* is same as $ls.

Q.1. List all the files whose names start with a vowel.
$ls [a e i o u]*
The meaning of this is the first character of the filename should be ‘a’ ‘e’ ‘i’ ‘o’ ‘u’ any one of them and the remaining can be anything.

Q.2. List all the files whose names start with a consonant or do not start with a vowel.

$ls [! a e i o u]*
! symbol complements the condition.

[ ] → square bracket always substituted by a single character but we can specify group
$ls [a – d] [f – x] [1 – 7] ? ? ?

This will display 6 character filenames whose first character range between a to d second character range between f to x and third character range between 1 to 7. While fourth – fifth and sixth are any valid characters.

Q3. List all the characters which end by the character e.
[br]
In which display the result if the last letter is e.
$ls -l

It will display all the files in the present directory including the files present in any subdirectories that may be present in the current directory. When this command interacts with any subdirectory then first list all files from the subdirectory before the next file in the current directory.

$ls -l
total 10
-rwxr – x – wx 1 root group 880 sept 09 10:05 file1
drwxr – x – x 1 root group 330 sept 19 10:07 mdir1

“Total 10” shows the total no. of disk block that the file in the current directory has occupied is 10. Unix treats all entities files, directories, and device as a file but it differentiates between all of them it uses file types. On which left most characters indicate this type.

File typeMeaning
–Ordinary file
bBlock specified file
cCharacter special file
dDirectory file
sSemaphore
mShare memory file

The next nine characters rwxr – x – wx show the permission. r – read, w – write, x – execute. After that each column in succession gives the number of links. Owner name, group name, size of the file in byte, date and time when the file was last modified and at last it gives filename. The nine characters following the file type character:

-rwxr – x — x

This is the combination of three entities. These entities are owner, group, and re (other, outside the group). The first three characters decide the permission for the owner and the three characters for the group and the next three for others.

  • the owner can read, write and execute the file.
  • the group can read and execute the file.
  • rest other can execute the file.

Their permission can be encoded numerically like

weight   permission
4                 r
2                w
1                 x

Example 1
rwx-wx–x => owner is 4 + 2 + 1 = 7
group is 0 + 2 + 1 = 3
other is 0 + 0 + 1 = 1
=> 731

Example 2
rwxrw–wx => owner is 4 + 2 + 1 = 7
group is 4 + 2 + 0 = 6
other is 0 + 2 + 1 = 3
=> 763

Touch command

$touch sample1

This command creates a file, size of the file would be zero bytes because touch does n allow us to store anything in a file. But we can create several empty files quickly.

$touch s1 s2 s3 s4 s5 s6 time. Touch command used to change the access time of sample1 to whatever is the current time.

$touch -a sample1

If want to set the access time for a file to a particular time instead of the current time.

$touch – a 0728110799 sample1

It has two digit each of the month, day, hour, minute, and year.

uname command

Uname finds out the name of the UNIX/Linux system we are using.

$uname
If we want to find out additional information like OEM (Original Equipment Manufacturer) number, release number, type of microprocessor, and no. of CPU present then we can use it.

-x option with uname
$uname -x

tty command

Find out the name of our terminal file.
$tty/dev/tty3a
means all the transactions with the Unix/Linux system is via the file/dev/tty3a. The output display on the screen of the monitor is picked from the file associated with the terminal. The try utility displays the path name of its standard input file if it is a terminal.

head command

head command assumes that we want to display the first 10 lines in the file.

$cat sample$head sample1$head – 3 sample1
zerozerozero
oneoneone
twotwotwo
threethree
fourfour
fivefive
sixsix
sevenseven
eighteight
ninenine
ten

tail command

The tail command displays the last part of a file tail command assume that we want to display the last 10 lines in the file.

$tail sample1$tail – 3 sample1
oneeight
twonine
threeten
four
five
six
seven
eight
nine
ten

pwd command

It is used for finding the present working directories for users.
$pwd
/usr/user2
The / denote root directory within this there is a subdirectory. usr within which there is another directory user2, where we are working right now.

grep command

grep command means globally search a regular expression and print it. This command is used to search the input in a specified file and display it on the screen. In the searching the pattern of grep we can use shell metacharacters and regular expressions. Metacharacter is “?”, ‘!’, ”, [ ].

Example 1. $grep How sample1
This command searches the word ‘How’ in the file sample1 and if the word is found the line containing it would be displayed on the screen.

Example 2. We can search the word How in more than one file.
$grep How sample1 sample2 The word ‘How’ would be searched in both files sample1 and sample2 and if this word is found then display on the screen the line containing the word as well as the name of the file.

Example 3. We can search for more than a single word, and for that single quotes can be used to enclose the same.
$grep How u’ -i -n sample1 sample2

This command search How u in file sample1 and sample2 which makes it case sensitive. -n option shows the no. of lines in which the pattern was found to be printed by the side of each line.

Example 4. $grep b?? n sample1
This command would display all four-letter words whose first letter is a ‘b’ and last letter is ‘n’ in which ‘?’ symbol represents one character each.

Example 5. $grep -v b* sample1
This display the output that does not contain words starting with ‘b’.

sort command

The sort command is used for sorting the content of a file. This command sort the content of a file according to ASCII sequence, therefore, it sorts the space and tab first then the number followed by uppercase and lowercase letter in that order.

Example 1. Use the sort command for file sample.
$cat > sample1
Hi how r u?
Ajay what is your rollno?
36
ctrl d
$sort sample1
36
Ajay what is your roll no?
Hi, how r u?

This command sorts the content of sample1 and display the sorted output on the screen

Example 2. We can sort the content of no of files in a single command line.
$cat sample2 abc def
$cat sample3
hi how r u
atul what r u doing
$sort sample1 sample2 sample3
36
abc
ajay what is your roll no.
atul what r u doing
def
Hi how r u
hi how r u

Example 3. If we want to store the sorted output in a file instead of displaying the sorted output on the screen.
$sort – o new sample sample1 sample2 sample3
This command sorts the three file and save the result in a file new sample. If we want the elimination of duplicate line from the given file then we use the – u option which output only unique lines.

Example 4. $cat sample1
hi how r u
hi how r u
abc
$cat sample2
abc
def
$cat sample3
def
ghi
$sort -u – o newsample sample1 sample2 sample3
abc
def
ghi
hi how r u

$sort command

In which no file is specified here, it is assumed that the input is to come from the standard input device.

$sort – file1

This command combines the content of the file with the input from the keyboard and carn out the sorting. Where stand for standard input i.e. the keyboard.

$mkdir newdir command

This command is used to create a directory named newdir.
$mkdir -p new1/new2/new3

In this command, we use option -p with mkdir which allow us to create multiple generations of directories. This will create all the parent directories according to the specified given path.

In the -p option tells the operating system to create new1 directory then new2 and within it new 3. We can create the directory with permission 777.

$mkdir -m 777 newdir
We assign permission for dir newdir is -rwxrwxrwx.

rmdir command

This command is used for removing the directory.
$rmdir newl/new2/new3

This command removes the directory new3.
$rmdir -p new1/new2/new3

In this command remove the parent directory of new 3.
$rmdir -i file

This command deletes the file and asked for confirmation before deleting the file.
$rm -r newdir

In DOS for removing a directory first empty the directory and then delete it. But in Linux/Unix single commands do the same work. This command removes all content of newdir and also itself.
$rm -f file1

This command will remove file1 forcibly.

Note: If the file name starts with, a then we use
$rm– -a or $rm./-a
$rm- a [This command also removes filea]

cd command

This command is used for changing over to a new directory.
$mkdir newdir1
$cd newdir1
After that, we will enter in newdir1.

If we give the cd command without any arguments is interpreted as a request to change over to the current user’s home directory.

cd: change directory
$pwd
/root
$cd /bin
$pwd
/bin
$pwd
dev
$cd bus
$pwd
dev/bus
if cd used without argument this revert to back homedirectory.

what is command

$whatis cp
cp cp(1) -copy file

This command also lists one line for a command man used with -f option to emulate whatis behavior.

type command

The easiest way to know the location of an executable program.
$type ls
ls is/bin/ls.

time command

The time command accepts the entire command line. It executes the program and also displays the time used on the terminal.

$time cat > file1
real 0.3
user 0.0
sys 0.1

real-time shown is the clock collapse time from its invocation of the command till its termination. user time showed as the time spent by the program in executing itself. sys indicates the time used by the Kernel in doing work on behalf of the user process.

CPU time

The sum of user time and sys time. CPU time is less than the real-time.

banner command

This command print a message in a large letters which looks like a banner. $banner HELLO.

This command may be split into two lines.
$banner “Hello”

This command displayed in the same line. The banner can accommodate only 10 characters in one line.

Exit command

This command is used to suspending sessions for the time being.
$exit
login:

clear command

clear is used to clearing the screen.

Uniq command

It locates repeated and nonrepeated lines. This command simply prints one copy of each line and writes it to standard output.

$cat file3
hi hello how r u
hi hello how r u
I am fine
I want to
I want to
pqr
sty
wxy
wxy
Z
$uniq file3
hi hello how r u
I am fine
I want to
pqr
str
wxy
z

But uniq command requires a sorted file as input. The general procedure is to sort a file and pipe its output uniq
$sort|file3|uniq

diff command

Connecting one file to another. This command explains which line in one file how to be changed to make the two files identical.

$diff file1 file2 or $diff file [12]
> hi cat > file1
> hi hi
hi
how
cat > file2
a
b
c
d
e
ch

$cat file3
A.B Raman
Chanchal
B.S Bhati
Sumit Upadhyay
$cat file4
A. Agarwal
Varun Yadav
A.B Raman
Lalit
B. Bhati
$diff file3, file4
0 al 2
> A Agarwal
> Varun Agarwal
2 < 4
< Chanchal
—
> Lalit
4 d 5
< Sumit Upadhyay
$diff file [3 4]
Append after line 0 of first line

Change line 2 of first line
Replace this line
with
this line
Delete line 4 of the first line
containing this line

diff uses special symbols and instructions to indicate the change that is required to make the file identical.

0 a1, 2 means appending two lines after line 0, which become lines 1 and 2 in the second file. 2 < 4 change line 2 to 4 in the second file and 4 d 5 delete line 4.

echo command

This command is used for displaying messages on the terminal and used for taking user input.

To display a message
$echo sun Solaris
sun Solaris

[br]
-> To evaluate shell variable

$echo $x
echo “enter your file name:\\”
enter your filename: $-

echo used with an escape sequence. It is generating a character string beginning with (back slash) – This placed the cursor and prompt in the same line and displays output.

There is some differences in echo behavior across the shell. echo behavior change according to the shell. In Linux bash shell interprets the escape sequence only when echo is used with the -e option.

echo -e “enter your file name”
enter your file name.

printf command

An alternative to echo or printf and echo are both used to displaying the output on the console. printf command is external but only the bash shell has the printf built-in. The printf command can be used in the same ways as an echo.

$printf “no filename entered \n”
→no filename entered

printf also accept all escape sequence used by the echo. printf also uses formatted strings in the same way the c language function of the same name uses them:
$printf “my current shell is %s \n” $shell
my current shell is user/bin/bash
In which printf replaces % s with the value of a $shell.
%s – string
%d – integer,
%f – floating point number.
%o – octal,
%x – hexadecimal integer

finger command

The finger command also lists the login detail of a user, When used without arguments it simply produces a list of all users logged on to the system.

$finger

LoginNameTTyIdleWhenWhere
herryherry khan*01*.y.com
sumitsumit kumar*0316sat 09:25saturn.abc.com
rootsuper user*0412sat 8:50

the second field shows the user’s full name, TTy shows the terminal, and proceed by an asterisk if the terminal have to write permission.

$finger sumit
login name: sumit
Directory     :/user/home/staff/sumit – shell: /user/bin/ksh
Or since Jan 13 12:58:37 on pts/4 from saturn.abc.com
Mail last read mon jan 17:24:37 2022
Finger also run on a network and it can obtain information about the remote user.

idle command

The time that has elapse since the last keystroke was entered at the terminal. The last field shows the name of the machine from which user logged in. The root user is local to the machine.

ps command

It shows process status & lists the currently running process (programs).

$ps
PID TTY TIME CMD
308 console 0:00 ksh
$ps -f
UID PID PPID C STIME TTY TIME COMMAND
user 227 1 2 12:48:10 2c 0:04 sh
user1 22577 227 12 16:11:40 2c 0:00 ps-f

kill: killing process

kill command send a signal usually with the intention of killing the process, kill is an internal command. This command used one or more PID as its arguments.

$kill 121 122 125 132

If we can run more than one job either in the background or in different windows in the xwindow system.

killing the last background job The system variable $! stores the PID of the last background job – The same PID number seen when the s is used with the command.

$sort -o emp emp1 s
345
Skill $!

Managing disk space

(i) df: disk free
The df (disk free) command repeats the amount of free space available for each file system separately.

$df
File system 1k-blocks use available use% mounted
/du/mapper/ 37455368 2014952 33507107 6% /
/dev/hdb1 101086 10789 85078 12% /boot
tmp fs 58820 0 58820 0% dev/shm

(ii) du: disk usage
10/dev
This command finds out the disk usage of a specific directory.

$du /root
1932 /root
$du/dev
1 /dev/disk/by label
2 /dev/disk/by-id
3 /dev/disk
5 /dev/pts
10 /dev

$du -s/dev: This display the only summary of the directory.
$du – a/dev/bus: du can report each file in a directory (-a option).
1 /dev/bus/vsb/001/001
1 /dev/bus/usb/001
2 /dev/bus/vsb
4 /dev/bus

Cancel command

It is used to canceling any job submitted by us.
cancel laser
cancel the current job on printer laser.

lpstat command

This command is used for obtaining the laser printer and job status. As well as lpstat show the status of all request submitted by the user who executed the command.
$lpstat -pprl can also use -pprl on laser
pprl -323 kumar 345670 jan
ppr -324 kumar 3659 jan

passed command

It is used to changing the password.
$passwd
changing password for user root
New UNIX password:
Retype new UNIX password:

cut command

cut – Remove sections from each line of files cut prints sections of each line of each input file, or the standard input if no files are given. A filename of – means standard input. The sections to be printed are selected by the options.

dirname command

dirname pathname

It is used for printing pathname, excluding the last level, and Useful for stripping the actual filename from a pathname. If there are no slashes in pathname, dirname prints “.” to indicate the current directory.

NAME command

dirname, basename – Parse pathname components

Synopsis
#include <libgen.h>
char *dirname (char *path);
char *basename (char *path);

The functions dirname and basename break a null-terminated pathname string into directory and filename components, dirname returns the string up to, but not including the final, and basename returns the component following the final. Trailing? characters are not counted as part of the pathname.

If the path does not contain a slash, dirname returns the string “.” while basename returns a copy of the path. If the path is the string “/”, then both dirname and basename return the string “/”. If the path is a NULL pointer or points to an empty string, then both dirname and basename return the string”.”.

Concatenating the string returned by dirname, a “/”, and the string returned by basename yields a complete pathname.

Both dirname and basename may modify the contents of the path, so if we need to preserve the pathname string, copies should be passed to these functions, dirname, and by subsequent calls. basename may return pointers to statically allocated memory which may be overwritten examples showing the strings returned by dirname and basename.

The following list for different paths:

path         dirname  basename
“/ usr/lib”  “/usr”  “lib”
“/ usr /”       “/”        “usr”
“usr”             “.”         “usr”

Example
char *dirc, *basec, *bname, *dname;
char *path = “/etc/passwd”;
dirc = strdup(path);
basec = strdup(path);
dname = dirname(dirc);
bname = basename(basec);
printf(“dirname=%s, basename=%s\n, dname, bname”);
free(dirc);
free(basec);

whereis command

It is used to locate the binary, source, and manual page files for a command, whereis attempts to locate the desired program in a list of standard Linux places.

whereis [ -bmsu ] [ – BMS directory… -f ] filename …
-b Search only for binaries.
-m Search only for manual sections.
-s Search only for sources.
-u Search for unusual entries.

A file is said to be unusual if it does not have one entry of each requested type. Thus ‘whereis -m -u *’ asks for those files in the current directory which have no documentation.

-B whereis searches for binaries.
-M whereis searches for manual sections.
-S whereis searches for sources.
-f Terminate the last directory list and signals the start of file names, and must be used when any of the -B, -M, or -S options are used.

Example

Find all files in/root/bin which are not documented in/root/man/man1 with source in /root/src:
$ cd/root/bin
$ whereis – u – M/root/man/man1 -S/root/sre -f *

which command

which – shows the full path of (shell) commands. Which takes one or more arguments. For each of its arguments, it prints to standard output the full path of the executable, which would have been executed when this argument had been entered at the shell prompt. It does this by searching for an executable or script in the directories, listed in the environment variable path.

which [ options ] [ — ] programname […]

options
–all, -a

Print all matching executables in PATH.

–read – alias, -i
Read aliases from stdin, reporting matching ones on stdout. This is useful in combination with using an alias for which itself. For example,

alias which =’alias | which i’.

–skip – dot
Skip directories in PATH that start with a dot.

–skip – tilde
Skip directories in PATH that start with a tilde and executable which reside in the HOME directory.

–show – dot
If a directory in PATH starts with a dot and a matching executable was found for the path, then print”/ programname”rather than the full path.

–show – tilde
Output a tilde when a directory matches the HOME directory. This option is ignored which is invoked as root.

-version, -v. – V
Print version information on standard output then exit successfully.

–help
Print usage information on standard output then exit successfully.

ll command
using ll command displays the long listing mode.
The format is:
$ll – htr

ll is an alias in most Linux distributions that stands for:
$ ls -l

So, instead of typing Is -1 all the time, we just type: ll.

If we came across the file system of Linux, we already know the description of file permissions and modification, access, and write dates, we must have known the other stuff that is only displayed using the long listing mode which is Is -l or ll.

h: It stands for output in human-readable format, the size, for example, would be in K or Giga or Tera accordingly.
t: It sort the most recently modified first. So we just look up to see the newest file in a directory Sort the file by modification time.
r: It reverses the sorting order, so we just look down to see the newest file in a directory.

w command

The command w displays the information about the users currently on the machine, and their processes. The header shows, 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, in same order.

options

h: Do not print the header.
u: Ignores the username while figuring out the current process and CPU times. To demonstrate this, do a “su” and a “w” and a “w -u”.
s: Use the short format. Do not print the login time, JCPU, or PCPU times.
f: Toggle printing the from (remote hostname) field. The default as released is for the from field to not be printed, although your system administrator or distribution maintainer may have compiled a version in which the from the field is shown by default
V: Display version information.
o: Old style output. Prints blank space for idle times less than one minute.
user, It Shows information about the specified user only.

By using the w command, we can see the information similar to this: 18.31 58 up 156 days, 5:16, 64 users, load average: 0.27, 0.27, 0.27 USER TTY FROM LOGING IDLE JCPU PCPU WHAT

logname command

The command logname displays the login name of the user. Unix knows each user by user and group identity numbers. These are distinct for each user.

$id (To print uid and gid)
uid = 201 (user 2) gid = 50 (group)

Computer Science Tutorials Linux computer scienceLinux

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions