Linux coding

Created on Sept. 15, 2012, 4:23 p.m. by Hevok & updated by Hevok on May 2, 2013, 5:07 p.m.

Welcome to the wonderful UNIX world. Here we are starting to learn your command-fu.

Why to use the command line? Because there is no better way to process multiple files, or multiple computers or multiple commands on multiple computers.

It is the way for massive file manipulation.

Terminal

The command terminal can be opened with Ctrl+Alt+t. Bash is the tool for running comamnd on the terminal. The first "word" of a bash command line is the name of a command or program. For instance less ./denigma/manage.py runs the program less which interprets ./denigma/manage.py as the path to a file that will be displayed in that terminal window. When the file is huge, less would show it a page at a time. space can be used to switch to the next page. q quits less.

~/ stands for home directory. With bash it is possible to set up variables and customizations in ~/.bashrc file, that way it will always load if a terminal session is initiated.

Navigating

The current location can be retrieved with: pwd (print working directory)

Path

echo $PATH shows all paths that are in the current environment. The current path can be extended via a command like: export PATH="/foo/bar:$PATH. This add /foo/bar to the beginning of the $PATH. This is only temporally. If this needs to be evked every time shell is started, put the command in ~/.profile.

Installing

To install packages/software use either apt-get or better aptitude (which is newer): ::

sudo apt-get install <package_name> # The classic standard way
sudo apt-get remove <package_name>
sudo apt-get install aptitude # Installs aptitude as an example.
sudo aptitude install <package_name> # you can use aptitude just as apt-get.

We can search for an available package like this: ::

sudo apt-cache search <package_name>

Multiple packages can be commanded to be installed in a single statement: ::

sudo apt-get install python-virtualenv python-django python-django-south python-numpy python-scipy

Updating

Individual package can be be updated/upgraded or everything on your system at once with this commands: ::

sudo aptitude udpate
sudo aptitude upgrade

Packaging

tar enables packaging: ::

tar czf new-tar-file-name.tar.gz file-or-folder-to-archive
  • tar - tar command
  • c - create new archive
  • z - compress the archive using gzip
  • f - use archive file.

It can be memorized with the phrase: "TARget Create Zip File

Extracting

To extract a package use for instance gunzip like this [http://www.cyberciti.biz/faq/howto-compress-expand-gz-files/]: ::

gunzip file.gz
gunzip < file.tar.gz | tar xvf-

Alternatively tar can be used: ::

tar xzf file.tar.gz
tar xzvf file.tar.gz # Shows the files being extracted during unpacking.

zip archived files are unziped like this: ::

unzip file.zip -d destination_folder

Editing

There are various light-weight editor build-in in many systems. The AND operator runs a application in the background [http://www.hashbangcode.com/blog/running-commands-background-linux-479.html].

nano file.txt
gedit file.txt
gedit file.txt & # Asynchronous opening (allows to continue work in the terminal in parallel while gedit runs in the background).
cp file.txt file.txt.bak% # Runs a file copy in the background

Searching

Unix provides a number of powerful built-in search capabilities. Among them are find, locate and grep.

find

Finding files in Unix is very simple: ::

find # Searches for files on system.
find . -name "*.txt" -mtime 5 # Find all files with .txt in the name that were modified in the last 5 days.

find / -type -mmin -10 # -f means normal files.
find ~ -iname "*xxx*" -exec mv -v {} /media/pr0n/ \ #~ home iname ' case insensitive #Moves all found files to media/pr0n [http://www.youtube.com/watch?v=x_R_JSiupzo&]feature=fvwrel]

locate

Locating files is extremely efficient as it uses indeces: ::

sudo updatedb # udpates the locate command.
locate <filename>
locate -i <filename> # -i makes the search non case-sensitive.

grep

grep allows to find (grab) a text string in files: ::

grep # Quickly finds text within files (even searching through subdirectories).
grep -ir "text string" * # Searches through all files in the current directory and below it for "text string".

which

The which command retrieves the location of an application, e.g.: ::

which python # /usr/bin/python

Using

To display the available memory run: ::

cat /proc/meminfo

To see the memory allocation and to free up unused resources run: ::

dmesg | grep memory

The df command allows to check out the disk filesystem usage: ::

df

To see a listing of system load use the commands w or top or (if installed) htop. top and htop are real-time, so it is possible to watch the load go up if video or other CPU intensive activities are ongoing.

Freeing up the cache:

The cache can be empties with a one liner [http://askubuntu.com/questions/155768/how-do-i-clean-or-disable-the-memory-cache]: ::

sync && echo 3 | sudo tee /proc/sys/vm/drop/caches

Environment Variables:

To see the current state of all environment variables print the env: printenv

If an unexpected error occurs like a killed python process it might be informative to check out the log files. On Debian and Ubuntu the main log file is at: /var/log/syslog When something happens that is exceptional as the kernel killing a process, there will be a log event explaining it.

Zooming

Desktop zooming capabilities can be activated. For this open the CompizConfig Settings Manager (which can be found under the dash home search GUI function). There Enable Enhanced Zoom Desktop and set a keyboard and/or mouse input you like the control zooming. For, instance you might set keyboard Zoom In to q and Zoom Out to z. IF zooming is either to slow or to fast adjust the Zoom factor and Minimum zoom factor. Note super is the key that often has a Windows symbol on it.

Terminal

A terminal can be instantly opened by pressing simultaneously Ctrl+Alt+t.

File/Folder Permission

chmode modifies the permission of files and folders via 3 digit code (OGA) as argument as the file or folder location. [http://mdshaonimran.wordpress.com/2010/06/13/chmod-change-filefolder-permission-in-ubuntu/] https://help.ubuntu.com/community/FilePermissions]

sudo chmod OGA folder/filer

O Owner (current user)
G Group (set by owner)
A Anyone else

Execute is 1, Write is 2 and Read is 4

0 - nor permission, this person cannot read, write or execute
1 - execute only
2 - write only
3 - execute and write only (1 + 2)
4 - read only
5 - execute and read only (1 + 4)
6 - write and read only (2 + 4)
7 - execute, write and read (1 + 2 + 3)

R means apply this permissions recursively to a ll files in the folder.

chmod -R xyz <filename>

chmod -R 741 <filename>

The last statement means: Set persons for the Owner to Read+Write+Execute (7); Set permission for Owner group to Read (4); Set permissions for Anyone else to execute only (1).

Unmask

Unmask is the way to set default permissions for file and directory creation. It is usually set for each user in their .profile or .login file, but it can be set at various times by just typing in for instance unmask 022, which will be active until it will be changed or user logs out.

The numbers to define unmask permission are the reverse of the chmod permissions. For instance if all files which will be created in the directory should have the permission 777 (read+write+execute for owner, group and anyone) set the unmask to 000.

Webcam

To use a webcam install cheese ($ sudo apt-get install cheese) and start it.

touch

touch is a standard Unix program used to change a file's access and modification timestamps. It can also be used to create a new empty file.

Killing

At the terminal a single running processes can be killed with Ctrl+C.

A specified number of processes are killable by applying: ::

ps aux | grep [process_name]
kill [number_of_processes]

All processes running under a name (e.g. python) can be easily killed, all at once with killall [process_name].

Shutdown

The system can be shutdown either by giving a countdown or right now: ::

sudo shutdown -P now # replace now by a countdown time in seconds if desired.

Tags: coding, programming, unix
Categories: Tutorial
Parent: Tutorials
Children: Install an RPM Package in Ubuntu

Update entry (Admin) | See changes

Comment on This Data Unit