Loading...
Basic Linux

Basic Commands In Linux

CommandDescription
#root user prompt
$user working prompt
$lognameDisplays current user name
$pwdPresent working directory
$dateDisplay current date & time
$calDisplay current month calendar
$cal 2022Particular year total months
$cal 02 20222022 year 02nd month calendar
$whoTo display the information about all the users who have logged into the system
$whoamiIt display current user name
$fingerIt displays complete information about all the users who are logged in
$uptimeHow long server up & running, how many users connected and load avg time
$which
or
$whereis
Given command location
Example: which date
$ttyTerminal position
$dfDisplay disk free size
$duDisk usage information
$clearTo clear the screen

Creating Files

cat (concatenate): It is used to create a file and display, appending the contents of the file.

To create a file:

$cat >  filename 
Welcome to Webnoid
ctrl + d (To save the file)

To display the content of the file:

$cat    filename

To append the data in the existing file:

$cat >>  filename
Webnoidschools
ctrl+d (To save)

Touch: It create multiple files but all are empty.
Syntax: $touch file1 file2 file3 ……. filen
Example:

$touch   file1    file2    file3

ls: Display the contents of a directory.
Syntax:

$ls   [options]

Options:
-r -> Reverse
-a -> hidden
-R -> Recursively
-i -> inode
-l -> lode list
-h -> human readable

mkdir: Created a directory.
Syntax:

mkdir    <Dirname>
$mkdir Linux

To create multiple directories:

$mkdir   Dir1  Dir2   Dir3 ...................  Dirr

To create a Nested directory:

$mkdir  -p    world/Asia/India/Telangana/Hyderabad

p is parent

To check:

$tree world

or

$ls   -R

Navigation Commands: (cd -Change Directory)

CommandDescription
cd /home/webnoidChanges current location to destination location
cd ..To go one level back
cd ../..To go two levels back
cdTo change user’s home directory

Example:

$cd   world
$ls

Note: The trailing slash (/) is optional when your are using the cd command. It indicates that the name being specified is a directory.
Example:

$cd   Documents/

cp: Copies files or directories from one location to another.
Syntax:

cp  [options]   source    Destination

-R -> Copies recursively
-f -> Copies Forcefully
-v -> Provides verbose output

Examples:

Copies one file to another:

$cp   file1    file2

Copies multiple files into Directory:

$cp   file1   file2      Documents

Copies one directory to another:

$cp     -R Documents unix
$cp    -rvf   Documents    unix    World/Asia/India/Telangana/Hyderabad
$cp   /var/log/messages    .



mv: Moves or renames files and directories.
Syntax:

mv   [options]    SOURCE      DEST

Options: -v -> verbose

Rename the file by specifying the file name & new name of the file:

$mv    messages    messages.bak

Move it to the test directory for safekeeping:

$mv    messages.bak     test/
$ls test

rm: Deletes files or directories.
Syntax:

rm    [options]   FILE

Options:
-i -> Interactive
-r -> Recursively
-f -> Forcefully

Delete the messages.bak file:

$cd   test
$rm -i messages.bak

Delete the test directory:

$cd     ..
$rm -rf test/

file: Displays the type of a file
Syntax:

file     <filename>

Example:

$file    test1
test1 : Empty
$file /etc/passwd
passwd : ASCII test

Meta Characters or Wild Card Characters

a) * : It matches zero or more characters in the given file.
Examples:

Displaying file start with ‘a’.

$ls    a*

Starts with t and end with g

$ls    t * g

Listout end with g only

$ls    *g

Removes starts with t

$rm    t*

Removes end with g only

$rs     *g

Copies start with a

$cp    a*     Documents/
$co     -rf       t*g     Documents/

Copies current directory all files

$cp      -ruf     *    /backup

b) ? : It matches any single character in the given file.
Example:

Display single character files.

$ls    ?

Display two character files

$ls    ??

List four character files but first one is ‘a’.

$ls    a???

Removes two character files.

$rm     ??  

Copies three character files.

$cp     ???

c) [ ]: It matches any single characters in the given list
Example:

Displays given matching character files.

$ls    [a e i o u]

Displays start with a, e, i, o, u files.

$ls   [a e i o u]*

Removing start with a, e, i, o, u

$rm    [a e i o u]*

Copying start with a, e, i, o, u

$cp    [a e i o u]*   unix/

d) [-] : It matches any single characters in the given range.
Example:

Displays starts with a, b, c, d, e, f, g

$ls   [a-g] *

Display start with a-e & p-v

$ls    [a-e, p-v] *

Removes a-e

$rm   [a-e] *

Copying files a-e & 0-9

$cp    [a-e, 0-9] *    unix/

Leave a Reply

Your email address will not be published. Required fields are marked *