Search For Answers
How do I copy files or directories in Linux?
Last updated: March 28, 2011
The cp command lets you copy files or directories in the command line interface in Linux.
The syntax for this command is:
# cp originalfilename nameofcopy
This is the most basic usage of this command and just makes a copy of a file in the same directory with a new name. This is a good way to make a backup of a file before you edit it so in case you make a major mistake you can easily delete the file you messed up and rename the backup back to the original name.
With cp you can also copy files from other locations in the filesystem to anywhere else in the filesystem.
For example if your current working directory is the root home directory you are in /root and if you want to copy a file from one directory to another you just use the absolute paths to those two locations in your copy command.
# cp /some/directory/originalfilename /some/other/place/nameofcopy
How do move or rename files or directories in Linux?
Last updated: March 28, 2011
The mv command lets you move or rename files or directories in the command line interface in Linux.
The syntax for this command if using it to rename a file is:
# mv filename newfilename
This is the most basic usage of this command and takes some getting used to at first as this is how you rename a file in Linux. You move the file to the same directory but with a new name.
The same goes for a directory rename:
# mv directoryname newdirectoryname
With mv you can also relocate files from your current working directory to anywhere else in the filesystem.
For example if your current working directory is the root home directory you are in /root and if you want to move a file from that directory to another you just use the absolute path to the destination in your move command.
# mv filename /some/other/place/filename
Or for a directory:
# mv directoryname /some/other/place/directoryname
How do I make a new directory in Linux?
Last updated: March 29, 2011
To make a new directory in Linux you use the mkdir command.
This is the syntax for this command:
# mkdir somenewdirectory
You can make that new directory anywhere you wish using an absolute path. For example if you are in the root user's home directory you are in /root but if you ant to make a new directory somewhere else in the filesystem use this syntax:
# mkdir /some/other/directory/newdirectoryname
How do I list the files in a directory in Linux?
Last updated: March 29, 2011
To list or view the files and contents in a directory in Linux you use the ls command.
The syntax for this command is:
# ls
This is the most basic version of this command and will just provide filenames in your current working directory in a linear format. This is ok if you just have a couple files but if you have lots of files it can be difficult to find what you are looking for. Don't worry you have lots of tools at your disposal to display those files and information about them in a variety of ways. To do this you use flags or options to the command and here are a few examples and what they do.
# ls -a (shows all contents in the directory even hidden files that start with a .)
# ls -l (shows the contents in a long list format)
# ls -S (sorts by file size)
# ls -R (lists subdirectories recursively)
# ls -t (sort by modification time)
Now the real power comes when you combine these options. For example if you want to list all the contents in your current directory in a list including hidden from largest to smallest with the sizes in human readable format (-h) use this syntax:
# ls -alhS
For more options check out the manual files:
# man ls
How do I delete a file in Linux?
Last updated: March 28, 2011
To delete a file in Linux use the rm command.
Use this syntax to remove the file:
# rm filename
You will be asked to confirm by entering yes or no.
How do I delete a directory in Linux?
Last updated: March 28, 2011
To delete a directory in Linux you use the rmdir command.
# rmdir directoryname
This command will only work on an empty directory. This is to protect you from deleting content within that directory.
To delete a directory immediately and all its contents which is called deleting a directory recursively you use the rm command with a custom flag.
rm -rf directoryname
This command can be VERY dangerous. I repeat. This command can be VERY dangerous!!
if you accidentally add an extra space before the directory name you could experience unwanted dataloss.
The f option will force the deletion without you having to ok each deletion within the directory
How do I see the last part of a file in Linux?
Last updated: March 28, 2011
If you want to see just the last part of a file when working in the command line interface in Linux you use the tail command.
To use the tail command use this syntax.
# tail filename
To view the last x number of lines of the file use the n option with a quantity declaration so for example if you wish to viw the last 500 lines of a large file you would use this syntax.
# tail -n 500 filename
An excellent option of the tail command is the f option. This allows you to view the live output of the file and see the latest lines get added to the file in real time. This is a powerfull tool for troubleshooting issues especially when using this to view log files. An example would be if you where having an isse with your system that happened when a specific action occurred you could tail the system messages file with the f option and then watch as that error was added as it happened.
Here is what that command would look like.
# tail -f /var/log/messages