Bash In Linux
Command Line Shell
A. Files and Directories
-
new directory:
mkdir {mynewdir}
-
go to directory:
cd {mynewdir}
-
show present working directory:
pwd
-
delete empty directory:
rmdir {mynewdir}
-
current directory:
.
-
parent directory:
..
-
create file:
touch {new.txt}
-
copy file:
cp {../my.txt} {.}
,cp {my.txt} {my2.txt}
-
move/rename file:
mv {my.txt} {empty.txt}
-
delete file:
rm *
,rm *.txt
,rm {empty.txt}
-
change permissions:
chmod {+x} {test.py}
-
change owner: chown {user} {filename}
chown {user1} {test.py}
-
change group: chgrp {group} {filename}
chgrp {user1} {test.py}
-
list entires in directory:
ls
,ls {mynewdir}
Add arguments (can join multiple, e.g.
-la
):- list all (inc. hidden) files
-a
- list directory entries only
-d
- human-readable format
-h
- print index number
-i
- long listing format
-l
- sort in reverse order
-r
- print size
-s
- sort by size
-S
- sort by modification time
-t
- list entries by lines
-x
- sort alphabetically by extension
-X
{permission} {no. of inodes} {owner} {group} {size} {date of mod} {filename}
-rwxrwxrwx 1 user1 user1 107 Jul 29 22:01 test.py
- list all (inc. hidden) files
B. File Operations
-
show content in std output:
cat {new.txt}
-
write file: nano/vim etc. {filename}
nano {new.txt}
-
count no. of characters, words, lines:
wc {new.txt}
-
text type:
file {test.py}
test.py: ASCII text, with no line terminators
-
print first 10 lines:
head {new.txt}
-
print last 10 lines:
tail {new.txt}
-
translate:
tr {' '} {'\n'}
-
sort:
sort
(alphabetically),sort {-nr}
(numerically reversed) -
terminal paging program (scroll through contents):
less
(quit withq
) -
Extract parts each line (by separator after
-d
) and print the field (of no. after-f
):cut -d{' '}, -f{1-} {new.txt}
(field counts from 1) (1-: from 1 onwards)
C. Additional Commands
- show manual of a command:
man {echo}
(quit withq
) - print to std output:
echo {Hello}
,echo {"Hello"}
- globally search for regular expression and print out:
grep {true}
- current date:
date
- list of logged in users:
who
- computer running duration:
uptime
- unused memory:
free
D. Redirect I/O Streams
-
redirect std output to new file or overwrite:
>
{./hello.py} > {hello.txt}
,{python hello.py} > {hello.txt}
-
redirect std output to new file or append:
>>