Useful Linux Commands
File
General File Operations
Append data to a file:
echo "linux" >> hello.txt
echo "world" >> hello.txt
To view only first two lines from the file:
head -2 hello.txt
Check statistics of file:
stat hello.txt
Display Disk Usage:
du -xh --max-depth=3 [file directory]
Verify file integrity:
md5sum [filename]
Hard Link:
ln hi.txt hello
Soft Link:
ln -s hi.txt hello
File Parsing
GREP
grep -r 'Hello' . # entire directory
grep -i 'lINUX' hello # case insensitive
grep -n 'linux' hello # display line number
grep -v 'word' hello # display lines that do not match pattern
Count line of files:
wc -l haha.txt
cut
cut -f3 -d' ' new.txt # f: column, d: delimiter
diff
diff [file a] [file b]
diff3 [file a] [file b] [file c]
change permission on all files and sub-directories
chown root:staff -R ~/dir2 # To change permission on all files and sub-directories
chgrp root file1.txt # change group
chgrp -hR root dir2
File Attribute
dirname
basename
To find regular files and invoke the file command on the results, run
find . -type f -exec file '{}' \;
To find regular files and display their attributes using the ls command, run
find . -type f -exec ls -l '{}' \;
To find files over 20 bytes in size and list them out, run
find ~ -type f -size +20c -exec ls -hl {} \;
Locate File and its type
file -s /dev/sda2
Basic Process Commands
“Process is nothing but a file-content which is residing in RAM”
Check process
ps
Kill process
kill [process id]
killall [process name]
killall -u [username] # kill process owned by user
killall -w find # Wait for all find process to die
Get the process ID of a running program bash:
pidof bash
pidof -s bash # returns only one process id
Process Priority
nice -n 19 sleep 30 &
-20
: most favorable, 19
: least favorable, only when nothing runs
Adjust Priority
renice -n 19 [pid]
renice +1 3176
3176: old priority 0, new priority 1
renice +4 3176
3176: old priority 1, new priority 4
To adjust priority for all process owned by a user “webminal”,
renice +1 -u webminal
Provide a dynamic real-time view of a running system
top
Display a tree of process:
pstree [pid]
pstree -p [pid] # display a tree of processes
how long it take to complete the program:
time ls -l
parent process
ps -o ppid,cmd 27447
Orphaned process are adopted by init which has pid 1:
ps S
PID TTY STAT TIME COMMAND
2249 pts/1 Ss 0:01 bash
3325 pts/1 R+ 0:00 ps S
Background processes
sleep 45 & # background process
jobs # list background jobs
fg 5 # make the fifth item of `jobs` foreground
sleep 3000
Ctrl + z # stop the job in foreground
bg # restart the job in background
System and User Details
uptime # how long this system has been up and running
who # display details about currently logged users
who -a # see other linux user (or w)
mount # display list of mounted file system
mount -t ext4 # view only ext4 file system
df -h # display free disk space on mounted devices
free -m # displays the total amount of free and used physical and swap memory in the system
MySql client
mysql
mysql -u yourname -p password
create database universe;
use db_yourname;
create table planets(name varchar(15), position int, has_moon boolean);
show tables;
describe planets; # view the format/structure of this table.do type
insert into planets values ('earth',3,1);
insert into planets values ('mars',4,2);
select * from planets;
select * from planets where has_moon=1;
select name,position from planets where has_moon=1;
update planets set position=2 where name="earth";
delete from planets where name="mars";
Scripting Introduction
source ./vars # sourcing a script
. ./vars # sourcing a script
bash -x runme # debug information
prevent setuid root spoofing
#!/bin/bash -
or
#!/bin/bash --
the script runs in the Korn shell:
#!/bin/ksh
test [ ]
The test command can test whether something is true or false.
[paul@RHEL4b ~]$ test 10 -gt 55 ; echo $?
1
[paul@RHEL4b ~]$
if then else
#!/bin/bash
if [ -f isit.txt ]
then echo isit.txt exists!
else echo isit.txt not found!
fi
if then elif
#!/bin/bash
count=42
if [ $count -eq 42 ]
then
echo "42 is correct."
elif [ $count -gt 42 ]
then
echo "Too much."
else
echo "Not enough."
fi
for loop
for i in 1 2 4
do
echo $i
done
with embedded shell:
#!/bin/ksh
for counter in `seq 1 20`
do
echo counting from 1 to 20, now at $counter
sleep 1
done
while loop
i=100;
while [ $i -ge 0 ] ;
do
echo Counting down, from 100 to 0, now at $i;
let i--;
done
until loop
let i=100;
until [ $i -le 0 ] ;
do
echo Counting down, from 100 to 1, now at $i;
let i--;
done
ACL
Mount ACL support:
mount -t ext3 -o acl _device-name_ _partition_
mount -t ext3 -o acl /dev/VolGroup00/LogVol02 /work
Set acl:
# setfacl -m _rules_ _files_
# setfacl -m u:andrius:rw /project/somefile
# setfacl -x _rules_ _files_ # remove permissions
# setfacl -x u:500 /project/somefile # emove all permissions from the user with UID 500
# getfacl home/john/picture.png
# file: home/john/picture.png
# owner: john
# group: john
user::rw-
group::r--
other::r--
User and Group Management Tools
- useradd, usermod, and userdel — Industry-standard methods of adding, deleting and modifying user accounts
- groupadd, groupmod, and groupdel — Industry-standard methods of adding, deleting, and modifying user groups
- gpasswd — Industry-standard method of administering the /etc/group file
- pwck, grpck — Tools used for the verification of the password, group, and associated shadow files
- pwconv, pwunconv — Tools used for the conversion of passwords to shadow passwords and back to standard passwords
useradd _<username>_
passwd _<username>_
groupadd _<group-name>_
usermod -L _username_ # lock password
chage -d 0 _username_ # Force immediate password expiration
usermod -p "" _username_ # assign a null password instead of an initial password