December 21, 2024

File Management Commands for Linux Server

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 /home/user/somedir

In Linux .. represents the parent directory and . represents the current directory. Therefore, if the current directory is /home/user/somedir, then cd ../somedir will not change the working directory.

The table below lists some of the most used file management commands:-

COPY CODE SNIPPET
Directory navigation
CommadUtility
pwdGet the full path of the current working directory.
cd – Navigate to the last directory you were working in.
cd ~ or just cd Navigate to the current user’s home directory.
cd .. Go to the parent directory of current directory (mind the space between cd and ..)
Listing files inside a directory
Command Utility
ls -lList the files and directories in the current directory in long (table) format (It is recommended to
use -l with ls for better readability).
ls -ld dir-name List information about the directory dir-name instead 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 executable, / means
directory, @ means symbolic link, = means socket, | means named pipe, > means door).
ls -ltList the files sorted by last modified time with most recently modified files showing at the top
(remember -l option 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/directory create, copy and remove
CommandUtility
cp -p source destination Will copy the file from source to destination. -p stands for preservation. It
preserves the original attributes of file while copying like file owner, timestamp,
group, permissions, etc.
cp -R source_dir
destination_dir
Will copy source directory to specified destination recursively.
mv file1 file2In Linux there is no rename command as such. Hence mv moves/renames the
file1 to file2.
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 dir-name recursive ly.
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 dir-name.
mkdir -p dir-name/dir-name Create 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/directory permissions and groups
CommandUtility
chmod <specificatio>filename
Change the file permissions. Specifications = u user, g group, o other, + add
permission, – remove, r read, w write,x execute.
chmod -R <specification> dir-nameChange the permissions of a directory recursively. To change 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 myfile.
chmod go -r myfileRemove read permission from the group and others.
chown owner1 filenameChange ownership of a file to user owner1.
chgrp grp_owner filenameChange primary group ownership of file filename to group grp_owner.
chgrp -R grp_owner dir-name Change primary group ownership of directory dir-name to group grp_owner
recursively. To change group ownership of a directory and everything within
that directory, use this command.

Vedant Kumar

Currently I'm working as an Implementation Engineer, Started my career as an System Administrator - Linux. Additionally loves to explore new technologies and research about new open-source software that ease the development cycle.

View all posts by Vedant Kumar →

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

close

Ad Blocker Detected!

VEDANT EXPLAINS
We've noticed that you are using an ad blocker. Advertising helps fund our server cost and keep it truly independent. It helps to build our content creator team. So please disable your ad blocker, and help us to keep providing you with free- great content - for free. Thank you for your support.

Refresh