Sie sind auf Seite 1von 7

Linux Commands for practice

~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Working with Files and Directories

Command :: List (ls)

ls <file name> or <directory name>

ls -a <file name> or <directory name>

ls -i <file name> or <directory name>

ls -l <file name> or <directory name>

ls -ll <file name> or <directory name>

ls -ld <directory name>

ls -ash

ls -n

ls -R

ls -m

-------------------------------------------

Change the current working directory to another directory

cd <Directory Name>

cd ..

cd ~

Absolute path

cd <specify path from the root directory />


cd /var/log/

Relative path
cd <specify path from the pwd directory />
cd log

-------------------------------------------------

copy files and directories

cp <source file> <destination file>

interative mode
cp -i <source file> <destination file>

recursive mode
cp -R <source file> <destination file>

preserve the file permissions


cp -p <source file> <destination file>

cp -irvf <source file> <destination file>

----------------------------------------------------

move (rename) files

mv <source file> <destination file>

interative mode
mv -i <source file> <destination file>

change file timestamps

touch <File Name>

change only the access time


touch -a <File Name>

change only the modification time


touch -m <File Name>

------------------------------------------------------

To View the File Content


cat - concatenate files and print on the standard output

cat <File Name>

cat -n <File Name>

TO append data to existing file

cat >> <File Name>

To creat a new file


cat > <File Name>
press Ctrl+D to save

---------------------------------------------------

Commands for Vim TextEditor


h moves the cursor one character to the left.
j moves the cursor down one line.
k moves the cursor up one line.
l moves the cursor one character to the right.
0 moves the cursor to the beginning of the line.
$ moves the cursor to the end of the line.
w move forward one word.
b move backward one word.
G move to the end of the file.
gg move to the beginning of the file.
`. move to the last edit
----------------------------------------

make links between files

Hard Link
ln <File Name> <Hardlink Name>

check its inode number


ls -all <File Name>
ls -all <Hardlink Name>

Modify the content in <File name>


check its update in <Hardlink Name>

Soft Link
ln -s <File name> <Softlink Name>

--------------------------------------------

umask

directory - 777

file - 666

For Root user umask value = 022


Default permission for directory 777-022=755

For Normal user umask value = 002


Default permission for directory 777-002 = 775

------------------------------------------------------

alias

alias <Name>='<unix command>'


alias hello='ls -all'

unalias <Name>

-------------------------------
Calender

Cal
cal 10 2013
cal 2013

-------------------------------
Statistics of the File or directory

stat <file name>

stat <directory name>

stat -f <directory name>

------------------------------------------

Lists anything that starts with File/Directory Name

ls <Name>*

Anything that ends with File/Directory Name

ls *<Name>

matches any filename containing the string anywhere in name

ls *<Name>*

matches filename with A and a


ls [Aa]<Name>

matches a range or sequence of characters


ls <Name>[a-c]

[a-z] matches any lowercase charactor

[A-z] matches all upper and lower case charactors

[0-9] includes all digit

-------------------------------------------------------

Find Command ---search for files in a directory hierarchy

~]# find -name <File Pattern>


~]# find -name sreenath

Like -name, but the match is case insensitive


~]# find -iname <File Pattern>

Descend at most levels levels of directories below the command line


~]# find <path> -maxdepth <level> -name <File Pattern>
~]# find / -maxdepth 2 -name passwd

~]# find / -mindepth 3 -maxdepth 5 -name passwd

find a file using inode


~]# find /var/ -inum <inode number> -exec ls -l {} \;

~]# find . -perm 755 -type f -exec ls -l {} \;


~]# find . -perm 755 -type d -exec ls -ld {} \;

~]# find / -size +10M

atime ---File was last accessed n*24 hours ago


~]# find -atime +1

-cmin n ----File's status was last changed n minutes ago.

-amin n ----File was last accessed n minutes ago.

-mmin n ----File's data was last modified n minutes ago.

~]# find /etc/ -name "*.conf" | xargs ls -l

------------------------------------

grep, egrep, fgrep - print lines matching a pattern

grep [options] PATTERN [FILE...]

~]# grep -i sreenath /etc/passwd

~]# grep -v sreenath /etc/passwd

~]# grep -c sreenath /etc/passwd

~]# grep -ir sreenath /etc/ (-R, -r, --recursive, Read all files under each
directory, recursively)

~]# grep "^root" /etc/passwd (Patterns starting with root)

~]# grep "nologin$" /etc/passwd (patterns ending with nolgin)

-------------------------------------------------

Schedule an "at job" using specific date and time

~]# at time date


~]# at 12:54AM Sep 30

job will be execute 1 minute from now.


~]# at now + 1 min

list all the pending at jobs


~]# atq

to delete the job


~]# atrm <JobID>

Example
at 2:35AM Jul 10
at> mkdir /tmp/sree
at> ctrl+D

--------------------------------

user and group administration

To create a new user ID


~]# useradd hero

To validate whether new UserID had been created or not


~]# tail -n 1 /etc/passwd
~]# tail -n 1 /etc/group

To change user password


~]# passwd hero

To check the user password


~]# tail -n 1 /etc/shadow

TO change the password ageing paramters


~]# chage -l hero

~]# chage -m 0 -M 90 -W 7 -I 14 hero


minimum password age [min days] (-m)
maximum password age [max days] (-M)
password warning period [warn days] (-W)
password inactive period [in active days] (-I)

To modify the new user paramters


~]# usermod -L hero ---(password lock)
~]# usermod -U hero ---(password unlok)

~]# useradd -m hero ---(create home directory)

~]# useradd -s /bin/bash hero ---(creat a login shell)

To create a new usergroup


~]# groupadd hollywood

~]# usermod -G hollywood hero

~]# cat /etc/group | grep -i hero (verify the group of user "hero")

To change the user defualt login shell


~]# chsh hero
/sbin/nologin (user login is restricted)
----------------------------------------------------------------

Standard input and output redirection


symbol example function

| cmd1 | cmd2 runs cmd1 and sends output to cmd2

> cmd > file Sends cmd output to file

>> cmd >> file Append ouput of cmd to file

< cmd < file takes input for cmd from file

2> cmd 2> errorfile sends standard error to errorfile

2>&1 cmd > msg cmd2>&1 sends standard output and error

-------------------------------

Das könnte Ihnen auch gefallen