Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 15

UNIX LIKE COMMANDS

http://www.smashingmagazine.com/2012/01/23/introduction-to-linuxcommands/
http://linuxcommand.org/

http://www.freeos.com/guides/lsst/
http://manuals.bioinformatics.ucr.edu/home/linux-basics
Get the description for a command (google search is easier! )
Man <commandname>

Install or Uninstall packages on your system


It goes to repository on the internet and find the program and install it.

sudo apt-get install libc6

Remove chromium, the Chromium web browser package


sudo apt-get remove chromium

upgrade a software
sudo apt-get upgrade softwarename

Clean the local repository of downloaded package files, clearing disk space.
sudo apt-get clean

top - display top CPU processes


sudo top

NOTE: All configuration files for linux are stored in a simple text file. That text file is:
/etc/passwd

Changing password
sudo passwd <username>

Adding and deleting a group


sudo groupadd <groupname>
sudo groupdel <groupname>

Adding and deleting user to a group


sudo adduser <username> <groupname>
sudo deluser <username> <groupname>

text file that stores the group information


/etc/group

Special features?
Basically, the "special feature" is the ability to give the user the special ability to automatically change
users or group, or to specify a directory as a "temporary" directory.
The "s" above for the group (middle) settings shows that the "sticky" or "setgid" bit is set. This means that
any user who changes into that directory suddenly performs all actions as if the "webmasters" group was
their default group. This can be helpful if you want all files in that directory to be created owned by that
webmasters group.
The "s" flag can also be set for the user, which makes the user "sticky" or "setuid". This is not usually a
good idea, so don't do it unless you really know what you're doing.
The "t" flag is basically the same thing as the "s" flag for user or group, but is used when applied to all
others. Here, the meaning is a little different. It means that anyone can create a file in the directory, but
only the owner is allowed to remove the file, regardless of permissions set. This is the "temporary"
directory permission and should also be avoided unless you really know what you're doing.

What does directory permission 'S' mean?


'S' = The directory's setgid bit is set, but the execute bit isn't set.
's' = The directory's setgid bit is set, and the execute bit is set.
SetGID = When another user creates a file or directory under such a setgid directory, the new file or
directory will have its group set as the group of the directory's owner, instead of the group of the user
who creates it.

Sticky bit
In computing, the sticky bit is a user ownership access right flag that can be assigned
to files and directories on Unix-like systems.

When a directory's sticky bit is set, the filesystem treats the files in such directories in a special way
so only the file's owner, the directory's owner, or root user can rename or delete the file. Without the
sticky bit set, any user with write and execute permissions for the directory can rename or delete
contained files, regardless of the file's owner. Typically this is set on the /tmp directory to prevent
ordinary users from deleting or moving other users' files.

Back to chmod
chmod actually has a couple ways to be used. There's a "named mode" which is a little easier to
comprehend and there's a "numeric mode" which exposes a little more of the guts.

Named Mode
Here we give names to all the involved parties. They come in two parts, a "who" and a "what."
who
u change the user bits
g change the group bits
o change the other bits
a change the bits for everybody
what
r

grant read access

w grant write access


x grant execute access
s set the sticky bit
Then, we use one or more from the first group combined with one or more from the second group and use
a plus ("+") or minus ("-") to glue them together to do what you want.
(+ used for add permission, - used for take away permission)

Examples
$ chmod a+r file.txt
This will allow everybody to read file.txt.

$ chmod go-rwx file.txt


This strips everybody of all permissions, except for the owner (who retains whatever permissions she had
before).
$ chmod ug+x script.cgi
The file named script.cgi is now executable by the user and group.
$ chmod g+s somedir
Now, all files created in the directory somedir will be owned by the group that owns somedir.

Using SSH To Log Into Your Web Server


ssh -l username www.myserver.com
ssh username@username.suso.org

The -l stands for log in as, and your user name goes after it. If SSH is allowed, then it will ask
for a password.

Change the directory to /, the top of the whole UNIX directory


structure
cd /

The current directory (.)


The parent directory (..)

The * wildcard
The character * is called a wildcard, and will match against none or more character(s) in a file
(or directory) name. For example, in your unixstuff directory, type
ls list*

This will list all files in the current directory starting with list....
ls *list

This will list all files in the current directory ending with ....list

The ? wildcard
The character ? will match exactly one character.
So ?ouse will match files like house and mouse, but not grouse.
% ls ?list

list directory
ls

shows permissions as well


ls l

shows dot files as well


ls a

shows dot files and permissions as well


ls la

chown and chgrp


Change the owner of /var/run/httpd.pid to 'root'
$ chown root /var/run/httpd.pid

Change the group of /var/run/httpd.pid to 'root'


$ chgrp root /var/run/httpd.pid

Change the ownership to newuser and group to newgroup for all of the files and directories in
current directory, and all subdirectories (recursively).
$ chown -R newuser:newgroup .

showing hidden files (in mac)


defaults write com.apple.finder AppleShowAllFiles YES
defaults write com.apple.finder AppleShowAllFiles NO

print working directory


pwd
/home/foobar

The who command prints information about all users who are
currently logged in.
who

will output the contents of a specific file


cat filename

more identical to cat but shows it slowly


more filename

"less" writes the contents of a file onto the screen a page at a time.
less science.txt

wc (word count)
A handy little utility is the wc command, short for word count. To do a word count
on science.txt, type
wc -w science.txt

To find out how many lines the file has, type


wc -l science.txt

show just the first few or last few lines of a file with
the head and tailcommands. (It shows 10 lines by default, but you can pass in
any number)
head index.html
tail -20 index.html

to rename this file


mv index.html indexold.html

to copy files
cp index.html indexold.html

http://www.hypexr.org/linux_scp_help.php
http://www.garron.me/en/linux/scp-linux-mac-command-windows-copy-files-over-ssh.html
http://unix.stackexchange.com/questions/2857/ssh-easily-copy-file-to-local-system
http://www.cyberciti.biz/faq/understanding-etcpasswd-file-format/

removing file from a directory


rm <filename>
rm Rf <filename>
(-r "recursive" -f "force" (suppress confirmation messages))

CHANGING THE PROMPT


You can change the prompt to whatever you want by setting the PS1 environment variable. Here
are a couple of examples, the latter including the user, host and current directory:
PS1="[woohoooo ]$ "
PS1='[${USER}@${HOSTNAME} ${PWD}]$ '

Searching For Files


You could download everything from the server using FTP and search the files using Finder or
Explorer. Or you could log in and search using the command line.
Start searching from the root directory (i.e / directory)
find /

If you just give it a directory, it will list everything that the directory contains.
find /var/www

If you have many websites, it could continue for a couple of minutes. You can stop it by hitting
Control + C. Thats the way to interrupt a Linux command.
The pipe symbol (|) takes the output of one command (in this case, the long list of files
produced by find) and passes it to another command (in this case, more, which shows you
one page of files at a time). As above, press the space bar to show the next page, and Q to quit.
find /var/www | more

To search for a specific file name, add -name and the file name. You can use * as a wild card (the
backslash is not always necessary but is good practice with the find command). You can combine
searches using -o (for or). If you leave out the -o, it becomes an and.
find /var/www -name "logo.gif"
find /var/www -name "*.gif"
find /var/www -name "*.gif" -o -name "*.jpg"

recursively find all files in current and subfolders based on wildcard


matching
find . -name "filename"

search by size
looking for all GIFs between 5 and 10 KB:
find /var/www -name "*.gif" -size +5k -size -10k

search by change
to find a file that was last changed between 90 and 180 days ago
find /var/www -name "*.gif" -ctime +90 -ctime -180

In both of these cases, you will probably also want to know the actual file size and date last
changed. For this, you can add -printf, which is similar to the C function printf in that you use
the % sign to output various information. This command outputs the file size (up to 15 characters
wide), the date and time changed (down to the nanosecond) and the file name:
find /var/www -name "*.gif" -size +5k -size -10k -ctime +90 -ctime -180 -printf "%10s

%c %p\n"

-cmin
Lets you see files that have changed in the last few minutes. If something goes wrong on a
website, you can run this to see everything that has changed in the last 10 minutes:
find /var/www -cmin -10 -printf "%c %pn"

This will show files and directories that have changed. Thus, it wont show files that have been
removed (because they are no longer there), but it will show the directories that they were
removed from. To show only files, add -type f:
find /var/www -cmin -10 -type f -printf "%c %pn"

Help from manual


man find

Looking Through And Editing Files


This will tell you which files in the current directory contain that bit of HTML.
grep "<div id="left">" *.php

You can use the -r option with a directory at the end, instead of a list of files. The single dot tells
it to start in the current directory.
grep -r "<div id="left">" .

You can also add -n to show the line numbers, as in this example:
grep -n "<div id="\content\">" *.php

Alternatively, you could use the find command from above to tell it which files to look in. To put
a command within a command, enclose it in back apostrophes. The following searches only for
the HTML in PHP files modified in the last 14 days:
grep "<div id="left">" `find . -name *.php -ctime -14`

fix an error
To do that, you will need to start up a Linux text editor. Different editors are available, such
as pico and emacs, but the one that is guaranteed to be there is vi. To edit a file, type vi and the
file name:
vi index.php
vim index.php //improved one!

To delete a character, press X. To delete a whole line, press DD. To insert a new character, press
I. This takes you into insert mode, and you can start typing. Press the Escape key when
finished to go back to command mode. Within command mode, type :w to save (i.e. write) the
file and :q to quit, or :wq to do both at the same time.

this will create a file with empty content in it whatsoever


touch directory/filename

this will create a directory


mkdir <directoryname>

To list all users you can use:


cut -d: -f1 /etc/passwd

To add a new user you can use:


sudo adduser new_username
OR
sudo useradd new_username

To remove/delete a user, first you can use:


sudo userdel username
Then you may want to delete the home directory for the deleted user account :
sudo rm -r /home/username

To modify the username of a user:


usermod -l new_username old_username

To change the password for a user:


sudo passwd username

To change the shell for a user:


sudo chsh username

To change the details for a user (for example real name):


sudo chfn username

File Permissions
The list of user names (and, thus, potential file owners) on a UNIX system is stored in the file
/etc/passwd. You can try:

more /etc/passwd

The Apache Web server is started by a command when the Web server boots up. But the
user who starts Apache is often a restricted and unprivileged user, such as nobody or apache or
www-data. This is for security reasons, to prevent someone from hacking into the website and
then gaining control of the whole server. You can find out who that user is by running the
command below and looking in the first column. The ps aux command shows all of the
processes running on the server, and grep shows only processes that contain the word
apache.
ps aux | grep apache

If you upload a file to a website via FTP and log in as admin, then the file will be owned
by admin. If Apache was started by the user named nobody, then Apache might not be able to
read that file and wont be able to send it to any users who request it when viewing the
website. Instead, users will see a broken image or a message such as 403 Forbidden. You
dont have permission to access that file.
A subtler and more common problem is when an image can be viewed but not overwritten
or removed via the websites content management system (CMS). In this case, the
user nobody can read the file but cant write to it.

Changing Permission Modes


chmod 644 <file>

To change the permission modes of all files or folders (use chmod in


tandem with the findcommand)
find . -type f -exec chmod 644 {} +

Or use this to change all of your folders to 755:


find . -type d -exec chmod 755 {} +

Standard server configuration:

You have a user account.

Your Web server runs as another user account.

Shared server configuration or suEXEC configuration:

You have a user account.

Other people who use the server have user accounts and might share the same
group with your user account.

Your Web server runs as the owner of your WordPress files.

The main difference between these two is in how the Web server runs.

PERMISSIONS FOR WORDPRESS (propably for


any php website)

All files should be 664.

All folders should be 775.

wp-config.php should be 660.

Heres what were trying to achieve with this set of permission modes:

Our user account may read and modify our files.

WordPress (via our Web server) may read and modify our scripts.

WordPress may create, modify or delete files and folders.

Other people may not see our database credentials in wp-config.php.

sudo find . -type f -exec chmod 664 {} +


sudo find . -type d -exec chmod 775 {} +
sudo chmod 660 wp-config.php

How to check which Apache group I can use for the web
server to write?
Apache runs as the www-data user but it can be different.
Use
ps aux | grep httpd
or
ps aux | grep apache

to see what user Apache is using on your system (The first column is apache)

http://stackoverflow.com/questions/18817744/chmod-all-files-to-644-andall-folders-to-755-of-a-dictory

chmod all files to 644 and all folders to 755 of


a dictory
find . -type d -print0 | xargs -0 chmod 0755 # for directories
find . -type f -print0 | xargs -0 chmod 0644 # for files

xargs clarification: xargs breaks the list of arguments into sublists small enough to be
acceptable.
rm `find /path -type f`
will fail with an error message of "Argument list too long" (meaning that the exec system call's limit on the
length of a command line was exceeded) if there are too many files in /path .

However, the version below (functionally equivalent to rm `find /path -type f` ) will not fail:
find /path -type f -print0 | xargs -0 rm

Finding all files containing a text string on


Linux
http://stackoverflow.com/questions/16956810/finding-all-files-containing-a-text-string-on-linux
Do the following:
grep -rnw 'directory' -e "pattern"
-r is recursive, -n is line number and -w stands match the whole word. Along with these, --exclude or -include parameter could be used for efficient searching. Something like below:
grep --include=\*.{c,h} -rnw 'directory' -e "pattern"
This will only search through the files which have .c or .h extensions. Similarly a sample use of --exclude:
grep --exclude=*.o -rnw 'directory' -e "pattern"
Above will exclude searching all the files ending with .o extension. Just like exclude file it's possible to
exclude/include directories through --exclude-dir and --include-dir parameter, the following shows how to
integrate --exclude-dir:
grep --exclude-dir={dir1,dir2,*.dst} -rnw 'directory' -e "pattern"
This works for me very well, to achieve almost the same purpose like yours.

OR
You can use grep -ilR:
grep -Ril "text-to-find-here" /
i stands for upper/lower case (optional in your case).
R stands for recursive.
l stands for "show the file name, not the result itself`.

You might also like