Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Notes regarding Linux Command for Revision, Study notes of Linux skills

Notes regarding Linux Command for Revision

Typology: Study notes

2020/2021

Uploaded on 02/22/2021

anokhi-bansal
anokhi-bansal šŸ‡®šŸ‡³

5

(2)

3 documents

1 / 34

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Linux Commands
pwd
Print Working Directory (shell builtin)
Syntax
pwd [-LP]
Options
-P : The pathname printed will not contain symbolic links.
-L : The pathname printed may contain symbolic links
The default action is to show the current folder as an absolute path.
All components of the path will be actual folder names - none will be symbolic links.
ls
List information about files.
Syntax
ls [Options]... [File]...
Key
Sort entries alphabetically if none of -cftuSUX nor --sort.
-a, --all Do not hide entries starting with .
-A, --almost-all Do not list implied . and ..
-c Sort by change time; with -l: show ctime
-d, --directory List directory entries instead of contents
-f Do not sort, enable -aU, disable -lst
-i, --inode Print index number of each file
-k, --kilobytes Like --block-size=1024
-l Use a long listing format
-L, --dereference List entries pointed to by symbolic links
characters specially)
-r, --reverse Reverse order while sorting
-R, --recursive List subdirectories recursively
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download Notes regarding Linux Command for Revision and more Study notes Linux skills in PDF only on Docsity!

Linux Commands

pwd

Print Working Directory (shell builtin)

Syntax pwd [-LP]

Options -P : The pathname printed will not contain symbolic links. -L : The pathname printed may contain symbolic links

The default action is to show the current folder as an absolute path.

All components of the path will be actual folder names - none will be symbolic links.

ls

List information about files.

Syntax ls [ Options ]... [ File ]...

Key Sort entries alphabetically if none of -cftuSUX nor --sort.

-a, --all Do not hide entries starting with.

-A, --almost-all Do not list implied. and ..

-c Sort by change time; with -l: show ctime

-d, --directory List directory entries instead of contents

-f Do not sort, enable -aU, disable -lst

-i, --inode Print index number of each file

-k, --kilobytes Like --block-size=

-l Use a long listing format

-L, --dereference List entries pointed to by symbolic links

characters specially)

-r, --reverse Reverse order while sorting

-R, --recursive List subdirectories recursively

-s, --size Print size of each file, in blocks

-t sort by modification time

-u sort by last access time; with -l: show atime

-U do not sort; list entries in directory order

-v sort by version

-1 list one file per line

--help display help and exit

--version output version information and exit

The most common options are -a (all files) and -l (long or details)

Examples

List the contents of your home directory

$ ls ~

list everything in a vertical list:

$ ls -al

total 109 drwxr-xr-x 18 root root 4096 Jun 9 21:12 ./ drwxr-xr-x 18 root root 4096 Jun 9 21:12 ../ drwxr-xr-x 2 root root 4096 Jun 9 21:14 bin/ drwxr-xr-x 3 root root 1024 Jun 9 20:32 boot/ drwxr-xr-x 6 root root 36864 Jul 12 10:26 dev/ drwxr-xr-x 34 root root 4096 Jul 12 10:25 etc/ ^ the first column is the file type d = directory f = file

List the directories in the current directory:

*$ ls -d /

list ALL subdirectories

**$ ls ***

The default behaviour of ls is to only pass color control codes to tty output --color=auto.

To pipe the output to a second command complete with color codes then set --color=always

cd

Change Directory - change the current working directory to a specific Folder.

Syntax cd [ Options ] [D irectory ]

-m Print the current user id, name and domain (Same as `who am i')

-q --count Print only the login names and the number of users logged on. Overrides all other options.

-i The who command differs from whoami in that it provides a list of all users currently logged into the system as well as additional information about each of those users (including login times and terminal numbers). It also differs in that, in the event of a change in ownership of a login session through the use of the su command, it reports the original owner of the session, whereas whoami provides the user name of the effective (i.e., current) owner of the session.

If given no non-option arguments, `who' prints the following information for each user currently

logged on:

login name,

terminal line,

login time,

remote hostname or X display.

If given one non-option argument, who' uses that instead of/var/run/utmp' as the name of the

file containing the record of users logged on. `/var/run/wtmp' is commonly given as an argument

to `who' to look at who has previously logged on.

If given two non-option arguments, `who' prints only the entry for the user running it

(determined from its standard input), preceded by the hostname. Traditionally, the two

arguments given are am i', as inwho am i'.

whoami

Print the current user id and name.

Syntax whoami [ options ]

Options --help Display Help --version Display program version info

whoami produces the same result as the id -un command (the id command by default provides

more detailed informatio

which

Locate a program file in the user's path.

For each of its arguments which prints to stdout the full path of the executable(s). It does this by

searching the directories listed in the environment variable PATH.

Syntax which [ options ] [--] program_name [...]

Options --all, -a Print all matching executables in PATH, not just the first.

--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'.

cp

Copy one or more files to another location

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Syntax cp [ options ]... Source Dest cp [ options ]... Source ... Directory

Key

-a, --archive same as -dpR

-b, --backup make backup before removal

-d, --no-dereference preserve links

-f, --force remove existing destinations, never prompt

-i, --interactive prompt before overwrite

-l, --link link files instead of copying

-p, --preserve preserve file attributes if possible

-P, --parents append source path to DIRECTORY

-r copy recursively, non-directories as files

--interactive Prompt whether to overwrite each existing destination file, regardless of its permissions. If the response does not begin with y' orY', the file is skipped.

--help display help and exit --version output version information and exit

Examples

Rename the file apple as orange.doc: mv apple orange.doc

Move orange.doc to the Documents folder: mv orange.doc ~/Documents/orange.doc

Rename a bunch of file extensions e.g. change *.txt into *.htm for f in *.txt; do mv ./"$f" "${f%txt}htm"; done

`mv' can move only regular files across filesystems.

If a destination file exists but is normally unwritable, standard input is a terminal, and the `-f' or

--force' option is not given,mv' prompts the user for whether to replace the file. (You might

own the file, or have write permission on its directory.) If the response does not begin with `y' or

`Y', the file is skipped.

rm

Remove files (delete/unlink)

Syntax rm [ options ]... file ...

Options -d, --directory unlink directory, even if non-empty (super-user only)

-f, --force ignore nonexistent files, never prompt

-i, --interactive prompt before any removal

-r, -R, --recursive remove the contents of directories recursively

-v, --verbose explain what is being done

--help display this help and exit

--version output version information and exit

To remove a file you must have write permission on the file and the folder where it is stored.

rm -rf will recursively remove folders and their contents

The OWNER of a file does not need rw permissions in order to rm it.

Undeletable files

The rm command accepts the `--' option which will cause it to stop processing flag options from

that point forward. This allows the removal of file names that begin with a dash (`-').

rm -- -filename

Alternatively use an absolute or relative path reference.

rm /home/user/-filename

rm ./-filename

To delete a file with non-printable characters in the name: `bad file name' Use the shell wildcard

"?" for each character

rm bad?file?name

Older file systems such as ext2fs, perform badly for massive bulk deletes. The newer ext3fs

doesn't have this performance problem.

To remove a very large number of files from a directory it can be quicker to rm them one at a

time in a loop:

find my_dir -type f | while read -r; do rm -v "$REPLY"; sleep 0.2; done

mkdir

Create new folder(s), if they do not already exist.

SYNTAX

mkdir [ Options ] folder ...

mkdir " Name with spaces "

OPTIONS

-m, --mode= MODE set permission mode (as in chmod), not rwxrwxrwx - umask -p, --parents no error if existing, make parent directories as needed --verbose print a message for each created directory

mkdir creates the standard entries. (dot) for the current folder

and .. (dot dot) for its parent

touch

Change file timestamps, change the access and/or modification times of the specified files.

touch sample.txt

Create/datestamp 10 files called file1, file2 etc:

touch file{1,2,3,4,5,6,7,8,9,10}

Create/datestamp 100 files called file1, file2 etc:

for i in $(seq 1 100); do echo -n "file${i} "; touch file${i} 2>&1; done

hostname

Print or set system name

SYNTAX

hostname [ name ]

With no arguments, `hostname' prints the name of the current host system. With one argument, it

sets the current host name to the specified string. You must have appropriate privileges to set the

host name

cal

Display a calendar

Syntax cal [-mjy] [[ month ] year ]

Options:

-m Display monday as the first day of the week.

-j Display julian dates (days one-based, numbered from January 1).

-y Display a calendar for the current year.

A single parameter specifies the 4 digit year (1 - 9999) to be displayed.

Two parameters denote the Month (1 - 12) and Year (1 - 9999).

If arguments are not specified, the current month is displayed.

A year starts on 01 Jan.

cat

Concatenate and print (display) the content of files.

Syntax cat [ Options ] [ File ]...

Concatenate FILE (s), or standard input, to standard output.

-A, --show-all equivalent to -vET

-n, --number number all output lines

--help display this help and exit

--version output version information and exit

With no FILE , or when FILE is -, read standard input.

Examples :

Display a file (this actually works by concatenating the file with STDOUT)

$ cat myfile.txt

Concatenate two files:

$ cat File1.txt File2.txt > union.txt

If you need to combine two files but also eliminate duplicates, this can be done with sort unique:

$ sort -u File1.txt File2.txt > unique_union.txt

Put the contents of a file into a variable

$ my_variable=cat File3.txt

mount

mount a file system

All files accessible in a Unix system are arranged in one big tree, the file hierarchy, rooted at /.

These files can be spread out over several devices. The mount command serves to attach the file

system found on some device to the big file tree.

SYNTAX

mount [-fnrsvw] [-o options [,...]] device | dir

OPTIONS

-a Mount all filesystems (of the given types) mentioned in fstab.

-h Print a help message.

-V Output version.

% exit

$ # now back in the bash shell

$ logout

echo

Display message on screen, writes each given STRING to standard output, with a space between

each and a newline after the last one.

Syntax echo [ options ]... [ string ]...

Options

-n Do not output the trailing newline.

-E

Disable the interpretation of the following backslash-escaped characters

-e Enable interpretation of the following backslash-escaped characters in each STRING:

\a alert (bell)

\b backspace

\c suppress trailing newline

\e escape

\f form feed

\n new line

\r carriage return

\t horizontal tab

\v vertical tab

\ backslash

\ NNN

the character whose ASCII code is NNN (octal); if NNN is not a valid octal number, it is printed literally.

\x nnn the character whose ASCII code is the hexadecimal value nnn (one to three digits)

echo is a BASH built-in command

wget

wc

Print byte, word, and line counts, count the number of bytes, whitespace-separated words, and

newlines in each given FILE, or standard input if none are given or for a FILE of `-'.

Syntax wc [ options ]... [ file ]...

Options

-c --bytes --chars Print only the byte counts.

-w --words Print only the word counts.

-l --lines Print only the newline counts.

-L --max-line-length Print only the length of the longest line per file, and if there is more than one file it prints the maximum (not the sum) of those lengths.

`wc' prints one line of counts for each file, and if the file was given as an argument, it prints the

file name following the counts.

If more than one FILE is given, `wc' prints a final line containing the cumulative counts, with the

file name `total'. The counts are printed in this order: newlines, words, bytes.

Examples

Count the number of lines in file1.txt

$ wc -l < file1.txt

Count the number of words in file2.txt

$ wc -w file2.txt

Count the number of words by pasting from the clipboard:

ibm' Convert ASCII to alternate EBCDIC.block' For each line in the input, output cbs' bytes, replacing the input newline with a space and padding with spaces as necessary.unblock' Replace trailing spaces in each cbs'-sized input block with a newline.lcase' Change uppercase letters to lowercase. ucase' Change lowercase letters to uppercase.swab' Swap every pair of input bytes. GNU dd', unlike others, works when an odd number of bytes are read--the last byte is simply copied (since there is nothing to swap it with).noerror' Continue after read errors. notrunc' Do not truncate the output file.sync' Pad every input block to size of `ibs' with trailing zero bytes.

The numeric-valued options (BYTES and BLOCKS) can be followed by a multiplier: `b'=512,

c'=1,w'=2, xM'=M, or any of the standard block size suffixes likek'=1024.

Examples:

Clone one hard drive onto another

$ dd if=/dev/sda of=/dev/sdb

Clone a hard drive to an image file

$ dd if=/dev/hda of=/image.img

Clone a hard drive to a zipped image file in 100Mb blocks

$ dd if=/dev/hda bs=100M | gzip -c > /image.img

Create a boot floppy:

$ dd if=boot.img of=/dev/fd0 bs=

test

Evaluate a conditional expression expr.

Syntax test expr [ expr

Options

Each operator and operand must be a separate argument. When the [ form is used, the last

argument to the command must be a ]. Expressions may be combined using the following

operators, listed in decreasing order of precedence.

! expr True if expr is false. ( expr )

Returns the value of expr. This may be used to override the normal precedence of operators. expr1 -a expr True if both expr1 and expr2 are true. expr1 -o expr True if either expr1 or expr2 is true.

The test and [ builtins evaluate conditional expressions using a set of rules based on the

number of arguments.

0 arguments

The expression is false.

1 argument

The expression is true if and only if the argument is not null.

2 arguments

If the first argument is `!', the expression is true if and only if the second argument is

null. If the first argument is one of the unary conditional operators, the expression is true

if the unary test is true. If the first argument is not a valid unary operator, the expression

is false.

3 arguments

If the second argument is one of the binary conditional operators, the result of the

expression is the result of the binary test using the first and third arguments as operands.

If the first argument is `!', the value is the negation of the two-argument test using the

second and third arguments. If the first argument is exactly `(' and the third argument is

exactly `)', the result is the one-argument test of the second argument. Otherwise, the

expression is false. The -a' and-o' operators are considered binary operators in this

case.

4 arguments

If the first argument is `!', the result is the negation of the three-argument expression

composed of the remaining arguments. Otherwise, the expression is parsed and evaluated

according to precedence using the rules listed above.

5 or more arguments

The expression is parsed and evaluated according to precedence using the rules listed

above.

True if file has its set-user-id bit set.

`-w file ' True if file is writable.

`-x file ' True if file is executable.

` file 1 -ef file 2' True if file 1 and file 2 have the same device and inode numbers, i.e., if they are hard links to each other.

File Age

These options test the file modification date.

` file 1 -nt file 2' True if file 1 is newer than file 2.

` file 1 -ot file 2' True if file 1 is older than file 2.

String tests

These options test string characteristics. Strings are not quoted for `test', though you may need to

quote them to protect characters with special meaning to the shell, e.g., spaces.

`-z String ' True if the length of String is zero.

-n _String_ ' String ' True if the length of String is nonzero.

` String 1 = String 2' True if the strings are equal.

` String 1 != String 2' True if the strings are not equal.

Numeric tests

Numeric relationals. The arguments must be entirely numeric (possibly negative), or the special

expression `-l STRING', which evaluates to the length of STRING.

ARG1 -eq ARG2'ARG1 -ne ARG2' ARG1 -lt ARG2'ARG1 -le ARG2' ARG1 -gt ARG2'ARG1 -ge ARG2' These arithmetic binary operators return true if ARG1 is equal, not-equal, less-than, less-than-or-equal, greater-than, or greater-than-or-equal than ARG2, respectively.

For example:

test -1 -gt -2 && echo yes => yes test -l abc -gt 1 && echo yes => yes test 0x100 -eq 1 error--> test: integer expression expected before -eq

Examples

$ ls -al total 67

drwxr-xr-x 18 root root 4096 Jun 9 21:12 ./ drwxr-xr-x 18 root root 4096 Jun 9 21:12 ../ -rw-rw-rw- 1 simon users 4096 Jun 9 07:30 london -rwsrwsrwx 1 simon users 4096 Jun 9 07:32 aberdeen -rw------- 1 simon users 4096 Jun 9 07:29 bristol -rw-r--r-- 1 simon users 4096 Jun 9 07:29 bath $

$ test -r paris

$ echo $?

Because the file `paris' is not Readable, the value returned is false (non zero)

if [ "$LOGNAME" = "scott" ] then echo "Logged in as Scott" else echo "incorrect user" fi

If the logname variable = scott then the test returns TRUE (0)

chmod

Change access permissions, ch ange mod e.

Syntax chmod [ Options ]... Mode [, Mode ]... file ...

chmod [ Options ]... Numeric_Mode file ...

chmod [ Options ]... --reference= RFile file ...

Options -f, --silent, --quiet suppress most error messages

-v, --verbose output a diagnostic for every file processed