Ubuntu-common commands

Ubuntu-common commands

Posted: 10 years ago in  Unix |


Ubuntu also has the GUI for normal usage just like Windows. However, its power is deeper inside the CLI-command line interface. At the first time I am with Ubuntu-Linux 12.04, I used GUI and tried to find applications to do same thing as on Windows.


Ubuntu also has the GUI for normal usage just like Windows. However, its power is deeper inside the CLI-command line interface. At the first time I am with Ubuntu-Linux 12.04, I used GUI and tried to find applications to do same thing as on Windows.

For example, "unzip file on Ubuntu", "something like IDM on Ubuntu", "something to search inline text like Total Commander on Ubuntu", bla bla bla... Most of them can achieved by using pre-installed softwares and command line in Terminal.

Especially, command line is very strong when dealing with permission, speed... I will share some of common things that make Ubuntu fabulous. All of them are not hidden secrets, not at all :-D

Note: this note written against Ubuntu 12.04 LTS

Open terminal + tab

To type in commandline, we use Terminal, shortcut key of opening new Terminal Window is Ctrl + Alt + T, of opening new tab in same window is Ctrl + Shift + T. Then, you are ready to go

Remote Connection to Ubuntu Server

Command line is excuted in local machine or remote machine. To connect to remote computer and input commandline, just like you sit in front of it, use SSH. remote computer in same local network

ssh username@local-ip-address

remote computer in different network:

ssh username@external-ip-address-or-domain-name-of-network

Then, input the password of that user Transfer file: SCP (secure copy) is two means to transfer file. They both requires password for connected user in remote computer

  • From local computer to remote computer (file)
    • scp <path-to-sourcefile> <username>@<IP address or domain-name>:<destination-to-destinationfolder><optional-new-filename>
  • From local computer to remote computer (folder)
    • scp -r <path-to-sourcefolder> <username>@<IP address or domain-name>:<destination-to-destinationfolder>
  • From remote computer to local computer (file)
    • scp <username>@<IP address or domain-name>:<path-to-sourcefile> <path-to-destinationfolder><optional-new-filename>
  • From remote computer to local computer (folder)
    • scp -r <username>@<IP address or domain-name>:<path-to-sourcefolder> <path-to-destinationfolder>

File Downloading:

It is called wget This is command to download using a link. e.g. this image link http://upload.wikimedia.org/wikipedia/commons/2/29/Linux_command-line._Bash._GNOME_Terminal._screenshot.png In Terminal, go to folder where we save file, then type in

wget http://upload.wikimedia.org/wikipedia/commons/2/29/Linux_command-line._Bash._GNOME_Terminal._screenshot.png

For batch download, copy all links into a text file, line by line, and use parameter -i in wget

wget -i /path/to/file.txt

If you want to download from https, run this command:

sudo apt-get install libxss1

Find file

Parameter: starting point (/path/to/folder) and filename (string between single quotes)

Search by file name

find /path/to/folder -name 'filename.extension' -type -f
find /path/to/folder -name 'foldername' -type -d

Search by file name (ignore case)

find /path/to/folder -iname ‘filename.extension'

Wildcard * to search with pattern

find /path/to/folder -iname '*.extension'

Limit search result by type: directory or file (d for directory and f for file)

find /path/to/folder -iname '*.extension' -type -f
find /path/to/folder -iname '*folder name*' -type -d

Find inline text

When we need to looking for file with declaration of method getName, then this is what you need:

grep "getName" /path/to/folder/

When you need a recursive search through all subfolders, then:

grep "getName" ./*

Cut Copy Paste Delete

Copy file:

cp /path/to/source/file.extension /path/to/destination/newfilename.extension

Copy folder:

cp -R /path/to/source/folder /path/to/destination/folder

Cut file:

mv /path/to/source/file.extension /path/to/destination/newfilename.extension

Cut folder:

mv /path/to/source/folder /path/to/destination/folder

Remove file:

rm /path/to/file/filename.extension

Remove folder:

rm -R /path/to/folder

Change permission

In Ubuntu, there are definition of Group, User and Permission as following:

When someone touch a file/folder, system check, respectively,

  • is owner ?
  • is in group groupname ?
  • is others

One file/folder will have permission list defining who will be allowed to to what: Owner, Group or Other

Permissions: Read (value 4) Write (value 2) and Execute (value 1). We use the sum of permission values to assign to a file.

Let take a look at example

Example:

User in system luan,tuan
Group in system marketing,developer
Filename settings.php
Owner of this file (owner:group) luan:developer

settings.php with permission: 7 7 7

  • anyone can read/write and execute this file

settings.php with permission: 7 7 5

  • only owner (luan) and people in group developer can read/write and execute, others can read and execute only

settings.php with permission: 7 5 5

  • only owner (luan) can read/write and execute, people in group developer and others can read and execute only

settings.php with permission: 7 5 0

  • only owner (luan) can read/write and execute, people in group developer can read and execute, others has no permissions and cannot even see this file

Here is how to assign the permission to file:

sudo chmod 755 settings.php

And how to change the ownership of file (requires root permission)

sudo chown username:group name settings.php

if you want to assign permissions and ownership to folder and subfolders, then use -R parameter:

sudo chmod 755 /path/to/folder -R
sudo chown username:groupname /path/to/folder -R

Find running services

View all

top

Find specific service by name, e.g. looking for apache server

ps aux | grep apache

Kill running service by ID

when you use ps aux | grep, you will get something like

www-data  2860  0.0  0.1 534408 14572 ?        Sl   08:15   0:00 /usr/sbin/apache2 -k start

2860 is ID of running service, then, kill it

kill -9 2860

View storage capacity:

In Terminal, go to folder that need to know,then type

du -hs <path-to-folder>

-s will prevent detail output of every file and subfolder

-h indicate that output is human-readable (e.g. a folder 1 Megabyte, displayed as 1M instead of 1048576 byte)

To check the whole disk/partition, use df instead

df -h

Reverse search of command: This is way to quicklly retype recent command you used. Press Ctrl+R then type few words. It will suggest recent commands. We keep hit Ctrl+R for more suggestion

Unzip and zip:

.tar.gz is well-known extension for compressed file in Ubuntu. zip is also available. Compress file and folder

tar -zcvf tar-archive-name.tar.gz /path/to/folder

Extract file to current folder

tar -zxvf tar-archive-name.tar.gz

v: verbose-display output while processing x: extract c: compress f: the name of the archive appears next (right after these options) z: list all the files in a compressed archive and extract them

Install/search new package

  • Update repository
    • sudo apt-get update
  • Apply upgrades
    • sudo apt-get upgrade
  • Install package
    • sudo apt-get install packagename
  • Remove package
    • sudo apt-get remove packagename
  • Search package
    • sudo apt-cache search keyword