Basic Linux Terminal Commands
-
Adding a user
While logged in as root, type in the shell:replace username with the username you wish
adduser username
Deleting a user:
While logged in as root, type replace username with the user name you wish to delete.userdel username
Change login password
To set the password for a specific user, replace username with the name of the user. By typing simply:passwd username
To change the current password for the user you are logged in as:
passwd
Change directories
cd - Used to Change Directories
Usage: cd /[directory]
Example:cd /tmp
Example:
cd ../
(moves back one directory)
Move to your home directory:
cd
Check the man pages for more info:
man cd
-
Downloading a file using the terminal command wget
First cd to the tmp directory
cd /tmp
Example:
wget http://news.lime-it.us/uploads/system/site-logo.png
Copy files
cp - Used to copy files or directories from one location to another. We can use the above example for this.
Example:
cp /tmp/site-logo.png /var/www/httpdocs
[as httpdocs is your root web directory]
To move ALL files from one directory to another:
Example:
cp /tmp/files/* /var/www/httpdocs
(the “*” tells the machine to copy everything in /tmp/files to /var/www/)
Check your man pages for more information on the cp command**
man cp
-
Renaming files
mv - Command used to move or rename files
Usage: mv file location
Usage: mv filename::newfilenameExample:
mv tmp/site-logo.png /tmp/lime-it.png
Read the manual:
man mv
Moving files
mv - Used to move or rename files
Usage: mv file location (to move)Example:
mv index.html /var/www/index.html
For more information, in a terminal and read the manual pages, type:
man mv
-
Editing files
The default editor installed with most all distros of linux is VIM , orvi filename
Vim is an advanced command line editor, you should consider learning the commands.
You can however also use nano To edit a file with nano, simply run:
nano filename
This will open up the file and allow to you edit the file. At the bottom of the screen you will notice the various commands that you have access to with nano. If you wish to save the file and exit, simply hold down the control key and hit the x key on your keyboard.
You will be asked to confirm this edit, if you have permissions to do so, once you confirm by typeing y, the file will be edited and nano will exit.
-
Deleting files
To delete a file you must first have write permission to it. Once you have write permission, in a terminal run:
rm filename\
There is no “Recycle Bin” in Linux so once you delete a file, it’s gone for good.
Being safe, find the files first and actually look at what you will be deleting!
find . -type f -name foo\*
When removing files, you may use an astrix “*” as a wildcard flag to remove certain files, for example if I wanted to remove all files that began with “foo” , I would run:
find . -type f -name foo\* -exec rm {} \;
If anyone tells you to run rm -rf / as root, DO NOT LISTEN TO THEM. Running this command will delete all the files/directories on your Linux system. And render the machine completely useless with zero chance of recovery!
You should not be running as root to begin with! Learn to use a secondary user, and as well the “su” command or “Super User” as general usage dictates.
Deleting directories
If you have ownership to the directory and the directory is empty, you can simply typermdir directoryname
to remove the directory.If the directory is not empty and you wish to simply delete it and all its contents, run:
rm -rf directoryname\[/code\]
Please be careful with the -rf flag, as it will remove everything in the specified directory including sub directories. With root access and the rm -rf command you can wipe out your entire system if you make an error. -
Listing directory contents
ls
- To list the contents of a directoryUsage:
ls [flags]
Example:
ls
(To list the current directory)Example:
ls /tmp
(To list the contents of /tmp)Example:
ls -a
(To list hidden files)Example:
ls -l
(To list file/directory permissions and file sizes)Example:
ls -al /tmp
(To list all files and permissions in /tmp)Example:
ls -il
(To get the inode number, permissions, owner, file sizes, UID and timestamps of all files in current directory)For more information, in a terminal, type man ls