CATCH MEKONG Training Day 1 – Linux Fundamentals
Linux Fundamentals
Fundamental Commands
cd <path>: change directory to given path
cd test01: change directory to test01
cd ..: go up one level to parent directory
cd: without any parameter you will go back to your home directory
mkdir: make a new directory
mkdir <directoryname>: creates a new directory with name <directoryname>
ls: list directory contents and file attributes.
ls –l: this command shows permissions, owner and group of a file, the file size, when it was
created and the name.
drwxrwxrwx 3 www-data www-data 4096 Apr 22 16:46 tmp
-rw-r--r-- 1 root root 27260 Nov 30 2012 wisdom_dep.log
drwxr-xr-x 2 www-data www-data 4096 May 10 2012 wps
mv: renames or moves a file or directory
mv <oldname> <newname>: rename filename or directory name
mv /path/directory /other/path/directory2: move directory to directory2
rm: remove file or directory (use carefully!!)
rm –r: removes a directory and all its content
cp: copy file or directory
cp –r <inputdirectory> <targetdirectory>: copies a directory and all its content
cp –rp <input> <target>: copies a directory and all its content, so that all file permissions stay
the same
touch: create a new, empty file
touch newfile.txt: if the file newfile does not exist, it will be created
CATCH MEKONG Training Day 1 – Linux Fundamentals
cat: This command can be used to
cat filename: Display text files on screen, if file with <filename> does not exist, then the
file with name <filename> is created.
cat filename > filename_copy: Copy text files
cat filename1 filename2 > filename_combined: Combine text files
ip addr: shows network configuration of the Linux machine (CentOS)
eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP
group default qlen 1000
link/ether 08:00:27:ec:f9:c8 brd ff:ff:ff:ff:ff:ff
inet 192.168.42.42/24 brd 192.168.42.255 scope global noprefixroute eth1
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:feec:f9c8/64 scope link
valid_lft forever preferred_lft forever
ip –s addr: outputs further information about network traffic, package loss, etc.
RX: bytes packets errors dropped overrun mcast
1686196 11149 0 0 0 0
TX: bytes packets errors dropped carrier collsns
46004719 26829 0 0 0 0
| : the pipe operator
redirects the output of one command to another command. You can use the pipe operator to chain
commands, e.g. to search for a certain lines in a file:
>> cat test.txt | grep “hello”
Terminate wrong commands
To stop a process that is taking too long to execute or is not working as expected, press ctrl + c
nano: a text editor
Nano is a very simple text editor. You can use it easily to write text to a file from command line.
>> nano test.txt
Use the arrow keys to move up and down in the file
Write what you want to write
Save your changes with ctrl + o
Exit nano with ctrl + x
more: allows paging through text
If you have a command that produces so much output that it does not fit on the screen (e.g. ps aux)
then you can use the pipe operator and more to be able to read the output page by page.
>> ps aux | more
>> more test.txt
With <enter> you can read line by line, with <space> the next page is loaded.
CATCH MEKONG Training Day 1 – Linux Fundamentals
less: allows forward and backward movement through text
Opens file in a window? that can be closed with <q>.
chmod: change file permissions
Changes file permissions on a file or directory. The command can be used recursively. Some
examples:
chmod u+r test.txt: adds read permissions to owner of the file
chmod o-w test.txt: removes write permission from all other not being the owner, or
member of the group.
chmod –R g+rw test01/: recursively add read and write permission for the group on the
directory test01 and all files and directories that are within test01
chown: change owner of file or directory
chown root test.txt: change owner of file test.txt to root
chown verena:root test.txt: change owner to verena and group to root for file test.txt t
chown –R root test01/: change owner to root for all files and directories in test01
df –h: report file system disk space usage in readable format
du: estimate file space usage
du –hs: recursively loops through all the directories in current directory and prints out the
file space usage. This is not an accurate number, but a good guess!
htop: interactive process viewer:
You see all cpus, memory usage etc. Leave with <q> (for quit).
If it is not installed you can download the htop package:
>> yum install htop
CATCH MEKONG Training Day 1 – Linux Fundamentals
grep: search in files
grep –l „hello“ *: search for “hello” in all files of current directory and give back the
filename of found matches. grep is case sensitive by default.
grep –i “hello” test.txt: is case insensitive and searches for “hello” in test.txt.
ps: list current processes
ps aux: list all running processes
ps aux | grep <processname>: list all running processes and search for the process with
name <processname>
Install Software
1. Check for updates with the command
yum check-update
2. Update the whole system:
yum update
3. to install new software, you need to know the package name and then you type
yum install <packagename>
4. for example, install the package httpd (apache web server):
yum install httpd
5. you can also install more than one package in one command:
yum install vim htop ssh
CATCH MEKONG Training Day 1 – Linux Fundamentals
Observe Processes
Check if SSH process is running:
me@mekongvm1:~# ps aux | grep ssh
root 5517 0.0 0.1 5504 972 ? Ss 18:36 0:00 /usr/sbin/sshd
root 5526 0.0 0.5 8268 2784 ? Ss 18:36 0:00 sshd: verena [priv]
verena 5529 0.0 0.2 8268 1472 ? S 18:36 0:00 sshd: verena@pts/0
verena 5583 0.0 0.1 1784 588 pts/0 S+ 18:44 0:00 grep ssh
Check if postgreSQL Database process is running:
me@mekongvm1:~# ps aux | grep postgres
postgres 1142 0.0 0.1 102332 6824 ? S May15 0:08
/usr/lib/postgresql/8.4/bin/postgres -D /local/pgsql/data -c
config_file=/etc/postgresql/8.4/main/postgresql.conf
postgres 1161 0.0 0.0 102332 1908 ? Ss May15 0:15 postgres: writer
process
postgres 1162 0.0 0.0 102332 1528 ? Ss May15 0:09 postgres: wal
writer process
postgres 1163 0.0 0.0 102468 1816 ? Ss May15 0:04 postgres:
autovacuum launcher process
postgres 1164 0.0 0.0 73880 1512 ? Ss May15 0:05 postgres: stats
collector process
postgres 1243 0.0 0.2 112592 14632 ? Ss May15 0:00 postgres: wisdom
prototype 127.0.0.1(41160) idle
postgres 1455 0.0 0.1 119508 6548 ? Ss May15 0:00 postgres: wisdom
prototype 127.0.0.1(41163) idle
root 14747 0.0 0.0 7548 832 pts/0 S+ 14:27 0:00 grep postgres