Change - Linux coding

Created on Sept. 15, 2012, 4:23 p.m. by Hevok & updated on Nov. 6, 2012, 10:36 a.m. by Hevok

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 computer. ¶

It is the way for massive file manipulation. ¶


Installing ¶
---------- ¶
To install packages/software use either apt-get or better <a href="http://superuser.com/questions/42967/on-ubuntu-why-sudo-apt-get-sometimes-and-sudo-aptitude-other-times">]aptitude</a> (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 <zip_filename> <filename> ¶


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 ¶

###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

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. ¶


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.


Comment: Added env variables printing.

Comment on This Data Unit