11679-NNK2RM

Introduction

In the most of our tutorials we rely on our readers and their at least the basic  knowledge about UNIX commands. If you are not too familiar with UNIX commands this one is for you.

Directories

Files and directories in UNIX use slash / as a separator. For example /home/jhon/file.sh means: directory “jhon” under directory “home” with the specific file “file.sh”.

Examples:

/              "root" directory
/home          directory home (sub-directory of "root")
/home/JHON     JHON is a subdirectory of /home

pwd – shows “present working directory”

Example:

[email protected] [/home/master]# pwd
/home/master

cd – change current directory

Example:

cd home           Change current directory to home which is a sub-directory of the current directory
cd ..             Change current directory to the parent directory of the current directory

ls – list information about the files in (current) directory

Example:

ls
directory   index.php   sendmail.php
ls -l
drwxr-xr-x    4 jhon    www-user        1024 Aug 8 20:11 directory
-rw-r--r--    1 jhon    www-user      767392 Aug 6 22:13 index.php
^ ^  ^  ^     ^   ^       ^              ^      ^    ^      ^
| |  |  |     |   |       |              |      |    |      |  
| |  |  |     | owner   group          size   date  time    name 
| |  |  |     number of links to file or directory contents
| |  |  permissions for world
| |  permissions for members of group
| permissions for owner of file: r = read, w = write, x = execute -=no permission
type of file: - = normal file, d=directory, l = symbolic link, ...

ls -a  #list including hidden files. Hidden files start with "." for example .htaccess

mkdir – make a new directory

Example:

mkdir example

Working with files (copy, move, rename, remove)

copy – copy file

Example:

cp file1.php file2.php #create copy of file1.php
cp /home/doe/file1.php /home/jhon/file2.php #create copy of file in different directory

mv – move file

Example:

mv /home/doe/file1.php /home/jhon/file1.php #move a file file1.php to different directory

mv – (again) rename file

Example:

mv file1.php file1.php #rename file1.php to file2.php

rm – remove file

Example:

rm file1.php   #remove file1.php
rm -r dir      #remove directory with content
rm -rf dir     #remove directory without prompt

Changing file permissions and attributes

chown – makes user:group the owner of file

Example:

chown user:group file1.php   #change user and group of a file1.php

chmod – changes the permissions of file

Example:

chmod 0777 file1.php   #change permissions for file1.php

Here is a little chown calculator for you (thanks JavaScript Kit):

Permission Owner Group Other
Read
Write
Execute

Conclusion

Those are nine basic commands for better orientation in all UNIX systems or distributions (Mac OSX, Ubuntu, Fedora, CentOS, RedHat, …)

Join the community
Comments are closed.

Our sponsors

We’re social

New in Lab

New Collection

Sorry, nothing found

We're sorry, but the your query has no result.

New Tutorial

Sorry, nothing found

We're sorry, but the your query has no result.

Knowledge base