File Management Commands in Linux
Linux uses some conventions for present and parent directories. This can be a little confusing for beginners.
Linux uses some conventions for present and parent directories. This can be a little confusing for beginners. Whenever you are in a terminal in Linux, you will be in what is called the current working directory. Often your command prompt will display either the full working directory, or just the last part of that directory. Your prompt could look like one of the following:
user@host ~/somedir $ user@host somedir $ user@host /home/user/somedir $
which says that your current working directory is
.. represents the parent directory and
. represents the current directory.
Therefore, if the current directory is
cd ../somedir will not change the working directory.
The table below lists some of the most used file management commands.
Directory Manipulation Commands in Linux
pwdGet the full path of the current working directory.
cd -Navigate to the last directory you were working in.
cd ~or just
cdNavigate to the current user’s home directory.
cd ..Go to the parent directory of current directory (mind the space between
Listing Files Inside a Directory
ls -lList the files and directories in the current directory in long (table) format (It is recommended to use
lsfor better readability).
ls -ld dir-nameList information about the directory
dir-nameinstead of its contents.
ls -aList all the files including the hidden ones (File names starting with a
.are hidden files in Linux).
ls -FAppends a symbol at the end of a file name to indicate its type (
@means symbolic link,
|means named pipe,
ls -ltList the files sorted by last modified time with most recently modified files showing at the top (remember
-loption provides the long format which has better readability).
ls -lhList the file sizes in human readable format.
ls -lRShows all subdirectories recursively.
treeWill generate a tree representation of the file system starting from the current directory.
File or Directory - Create, Copy and Remove Commands in Linux
cp -p source destinationWill copy the file from
-pstands for preservation. It preserves the original attributes of file while copying like file owner, timestamp, group, permissions etc.
cp -R source_dir destination_dirWill copy source directory to specified destination recursively.
mv file1 file2In Linux there is no rename command as such. Hence
rm -i filenameAsks you before every file removal for confirmation. IF YOU ARE A NEW USER TO LINUX COMMAND LINE, YOU SHOULD ALWAYS USE rm -i. You can specify multiple files.
rm -R dir-nameWill remove the directory
rm -rf dir-nameWill remove the directory dir recursively, ignoring non-existent files and will never prompt for anything. BE CAREFUL USING THIS COMMAND! You can specify multiple directories.
rmdir dir-nameWill remove the directory
dir-name, if it’s empty. This command can only remove empty directories.
mkdir dir-nameCreate a directory
mkdir -p dir-name/dir-nameCreate a directory hierarchy. Create parent directories as needed, if they don’t exist. You can specify multiple directories.
touch filenameCreate a file filename, if it doesn’t exist, otherwise change the timestamp of the file to current time.
File or Directory - Permissions and Groups Commands
chmod < specification > filenameChange the file permissions. Specifications =
chmod -R < specification > dir-nameChange the permissions of a directory recursively. To change the permission of a directory and everything within that directory, use this command.
chmod go=+r myfileAdd read permission for the owner and the group.
chmod a +rwx myfileAllow all users to read, write or execute
chmod go -r myfileRemove read permission from the group and others.
chown owner1 filenameChange ownership of a file to user
chgrp grp_owner filenameChange primary group ownership of file
chgrp -R grp_owner dir-nameChange the primary group ownership of directory
grp_ownerrecursively. Use this command to change group ownership of a directory and everything within that directory.