Saturday, April 4, 2015

Useful Linux/Unix commands

List files from current folder:
ls -l

Get current shell:
echo $SHELL

Get present working directory:
pwd

Get content from Home directory:
ls ~

List files from current directory:
ls .

Navigate to home directory:
cd

Navigate to root folder:
cd /

Find out file type:
file [path]

Navigate to a "two words" folder:
cd word1\ word2
cd 'word1 word2'

Hidden file name example:
.hiddenfile

List all files(including hidden):
ls -a

Manual page for a certain command:
man <command>
man -k <search term>

Chain parameters example for listing all files and add kilo, bytes to size:
ls -alh

Create a new directory:
mkdir learning

Create a new directory with parameter to display the action and parameter to create all directories if needed:
mkdir -pv dir1/dir2

Remove empty directory:
rmdir dir

Create new empty file:
touch filename

Copy a directory (including subfolders and files) to a new one:
cp -r dir1 dir2

Copy a file:
cp filesrc filedest

Move a directory (including subfolders and files) to a new one:
mv dir1 dir2

Rename file and directory:
mv filedir filedirnew

Remove a file:
rm filename

Remove non-empty folder:
rm -r folder

Interactive remove a folder or file:
rm -i file

Edit file with vi:
vi filename

Enter insert mode vi:
i

Enter edit mode from insert mode vi:
ESC

Save and exit from vi:
ZZ
:wq

Save vi changes, but don't exit:
:w

Discard changes and exit:
:q!

View file using different command than vi:
cat filename

View large files:
less filename
Note: b - previous page; Space - next page; q - quit;

Add numbers to vi lines editing:
:set nu

Navigating file in vi, edit mode:
Arrow keys - move the cursor around
j, k, h, l - move the cursor down, up, left and right (similar to the arrow keys)
^ - move cursor to beginning of current line
$ - move cursor to end of the current line
nG - move to the nth line (eg 5G moves to 5th line)
G - move to the last line
w - move to the beginning of the next word
nw - move forward n word (eg 2w moves two words forwards)
b - move to the beginning of the previous word
nb - move back n word
{ - move backward one paragraph
} - move forward one paragraph

Delete commands in vi, edit mode:

x - delete a single character
nx - delete n characters (eg 5x deletes five characters)
dd - delete the current line
dn - d followed by a movement command. Delete to where the movement command would have taken you. (eg d5w means delete 5 words)

Undo changes in vi, edit mode:

u

Copy/Cut-Paste vi:

Position the cursor where you want to begin cutting.
Press v to select characters (or uppercase V to select whole lines).
Move the cursor to the end of what you want to cut.
Press d to cut (or y to copy).
Move to where you would like to paste.
Press P to paste before the cursor, or p to paste after.

Wildcards:
* - Zero character or many of characters
? - Just one character
[] - Range of characters






No comments:

Post a Comment