Low Orbit Flux Logo 2 F

Linux / Unix Commands

Linux Unix Commands

Here we will cover most of the basic Unix/Linux commands so you can start to feel at home in a Unix environment. Many, many commands exist and will vary based on the exact system you are using and what packages are installed.

After covering this you should feel comfortable working in a Unix / Linux command line environment. This does not cover every tool that exists. This guide will not cover every skill you need to be a good admin.

This guide is meant to be somewhat concise. It doesn’t go into detail or cover all of the nuances of each command.

Environment:

Absolute Basics

If you need help use the “man” or “help” commands:


man
man ls
help ls
type ls

Some shortcuts:


history    # show your command history
[up]       # show last command
[tab]      # auto complete a command or path

Show your current directory:


pwd

List of files in a directory:


ls           # files in current dir
ls -a        # show hidden files
ls -l        # file details
ls -lh       # file details human readable
ls -ltr      # details, by time, in reverse
ls -l *.txt  # files matchiung *.txt
ls -l *abc*  # all files containing "abc"
ls mydir1    # all files in mydir1

Change your directory:


cd mydir             # change to mydir
cd /home/user1/mydir # use absolute path
cd .                 # move to current dir
cd ..                # move 1 dir up
cd ../../dir1        # 2 dirs up, then into dir1
cd My\ Music         # move to dir with a space
cd ~                 # home directory
cd ~user2            # home directory of user2
cd -                 # previous directory

This shows how to Create and delete files and directories.



file  file1.txt     # check file type
touch file1.txt     # create file or change last
                    # written time if it exists                  

mkdir testdir1          # create dir testdir1
mkdir newdir/subdir     # won't work if "newdir" doesn't exist yet
mkdir -p newdir/subdir  # -p makes it work recursively

rm file1.txt        # delete file
rmdir testdir1      # delete dir, only works if empty
rm -rf testdir1     # recursively and force delete
                    # works for files and directories

Copying Files:


cp file1.txt file2.txt     # copy to a new file
cp file1.txt dir1          # copy into a directory
cp -R dir1 dir2            # copy dir ( needs -R )


Moving files:


mv file1.txt file2.txt          # move file ( basically renames it )
mv file1.txt dir1/sub3          # move into a directory ( keep file name )
mv file1.txt dir1/sub3/file2.txt     # move move into a directory with new name


NOTE - to rename a file, just use the ‘mv’ command to move the file to a different name.

Links:


ln original.txt othername.txt      # create a hard link
ln -s original.txt othername.txt   # create a soft link


Soft Links vs Hard Links

Soft Links vs Hard Links

Echo:


echo "hello world"

Viewing Files:


cat mytextfile.txt        # output contents of this file
cat file1.txt file2.txt   # concatenate these two files together and output the content
cat > filename
[ctrl]-data

head myfile.txt           # print first 10 lines of file
head -n 1000 myfile.txt   # print first 1000 lines of file

tail myfile.txt           # print last 10 lines of file
tail -n 1000 myfile.txt   # print last 1000 lines of file

tail -n 1000 -f myfile.txt   # print last 1000 lines of file and follow
                             # good for log files, shows updates to file as they come in

more myfile.txt  # page through a file ( press space to see more )
less myfile.txt  # same as more but better ( more features like going back )


Commands to navigate through a file while using the “less” command:

[Page Up] Scroll back one page
b Scroll back one page
[Page Down] Scroll forward one page
[space] Scroll forward one page
G Go to the end of the text file
1G Go to the beginning of the text file
/characters Search forward in the text file for an occurrence of the specified characters
n Repeat the previous search
h Display a complete list less commands and options
q Quit

grep someinfo test.txt      # search for a string in a file
grep -i someinfo test.txt   # case insensitive
grep -v someinfo test.txt   # exclude instead of include
grep "abc\|xyz" test.txt    # match "abc" or "xyz"
grep -r someinfo *          # recursivly search all files
grep -r someinfo            # same but location is optional


Sort the lines in a file and output them:


sort test.txt       # sort in order
sort -r test.txt    # sort in revers
sort -n test.txt    # sort in string numerical order


Pipes and Redirects

Pipes and redirects allow output to be either piped to another command or redirected to a file.

| Pipe output from one command to another
> Redirect output and overwrite destination file.
>> Redirect output and append to end of destination file.
&>  

The output of one command can be sent to another using a pipe “|”. For example we can pipe the output of the ls command to the sort command.


ls | sort -r    # list files, then sort them


When you pipe output to grep, it will search this instead of a file. This becomes even more useful with some of the commands that we will learn later on in this guide.


ls | grep someText


~ home

Intermediate Commands

clear bc jobs / bg cal chgrp md5sum dd if=/home/tecmint/kali-linux-1.0.4-i386.iso of=/dev/sdc1 bs=512M; sync eject /dev/cdrom env hwinfo ifconfig / other network commands route arp etc. netstat / iostat / lsof / fuser ionice lscpu lspci lsblk lsusb lshw nc nice pidof init pstree ssh scp sleep stat touch tac talk time tr watch which whereis xargs yes

adduser/useradd …. cron anacron at


locate newFile.txt
locate -i newFile.txt        # case insensitive
locate -i *something*blah*   # anything that matches these two words



find .....
find . -name *ones*
find . -type f -name *ones*
find . -iname *wild*


nano # a popular text editor these days pico # also popular jed # another editor vi # traditional Unix text editor, on almost every system that exists, tricky if you aren’t familiar with it emacs # similar to vi but with completely different commands, some people like this better


free -m
free -h



su - user2      # become user2 ( need user2's password unless you are root )
su -            # become root user ( need root passwd )

sudo somecommand        # run a command as root ( if you have permissions )
sudo su -          # use sudo to become root ( use your own password )



ps
ps -ef
ps -aux


NOTE about output ( user PID PPID )

top

e....
p ....

us: value is the CPU time the CPU spends executing processes for users, in “user space” sy: value is the CPU time spent on running system “kernel space” processes ni: value is the CPU time spent on executing processes with a manually set nice value id: is the amount of CPU idle time wa: value is the time the CPU spends waiting for I/O to complete hi: The CPU time spent servicing hardware interrupts si: The CPU time spent servicing software interrupts st: The CPU time lost due to running virtual machines (“steal time”)

PID: Process ID USER: Name of the owner of the process PR: Process priority NI: The nice value of the process VIRT: Virtual memory used by the process RES: Resident memory used by the process SHR: Shared memory used by the process S: Status of the process. See the list below of the values this field can take %CPU: the share of CPU time used by the process since last update %MEM: share of physical memory used TIME+: total CPU time used by the task in hundredths of a second COMMAND: command name or command line (name + options)

D: Uninterruptible sleep R: Running S: Sleeping T: Traced (stopped) Z: Zombie

nice renice

sar

commands to sort output

kill 1692 # kill a process with pid 1692


df
df -k
df -h
df -h /var

du data   
du data | sort -nr


NOTE:


tar xvf myPackage.tar        # extract, verbose, file
tar xvfz myPackage.tar.gz    # extract, verbose, file, ungzip
tar xvfj myPackage.tar.gz    # extract, verbose, file, unbzip
tar -cvf myarchive.tar myfolder/      # create a tar file
tar -cvfz myarchive.tar.gz myfolder/  # create a tar.gz file
tar -cvfj myarchive.tar.gz myfolder/  # create a tar.bz2 file
gzip somefile.txt
gzip -k somefile.txt    # keep original

zip
unzip



uname
uname -a
uname -s  # kernel name
uname -r  # kernel release
uname -v  # kernel version

hostname

uptime    # how long the system has been up and the load factor



w                    # who is logged in
who                  # who is logged in
last                 # show history of logins
whoami               # show your username
getent passwd user1  # get account info
id                   # show your username, group, etc.
finger  user1        # user info if installed


/etc/passwd /etc/shadow


shutdown       # shutdown in 1 min
shutdown now   # shutdown now
shutdown +10 Warning, the system will be shutting down in 10 minutes
shutdown 23:00 Warning, the system will be shutting down at 23:00
shutdown -c    # cancel a pending shutdown



reboot .....

halt


cut sed awk pr lp

permissions …..

0: No permission 1: Execute permission 2: Write permission 3: Write and execute permissions 4: Read permission 5: Read and execute permissions 6: Read and write permissions 7: Read, write and execute permissions

user / group / owner


chmod 765 example.txt    
chmod -R 765 example.txt
chmod 755 file.py
chmod u+rx file.py
chmod og-rwx file.py
....

chown user1:group1 file.py
....



passwd user1      # change passwd
passwd            # change root password by default
sudo passwd user2 # change someone else's password
sudo passwd       # change root's password ( when you're not root )
groups user1      # show groups user1 is in


Users:

root

diff file1 file2 uniq …..

exit [ctrl-d]

Package Management:

dpkg apt-get rpm yum more….

================

ping ifconfig -a ip a tcp dump

wget curl

/ /etc /home /var /usr …..

/etc/passwd /etc/sudoers …..

alias cls=clear alias ll=”ls -l”

.bashrc .profile

cron at

system calls

ssh sshd telnet /etc/services

traceroute host nslookup

single line for loop

References:

More Linux Command Info