File Management
Managing Files in Linux
’’’ describe the file system ’''
File Commands
ls
The ls
command is used to list the contents of a directory. By default, it will list the contents of the current directory. To list the contents of a different directory, specify the directory as an argument to the ls
command.
ls
ls /home
cd
The cd
command is used to change the current working directory. To change to a directory that is a subdirectory of the current directory, specify the name of the subdirectory as an argument to the cd
command. To change to a directory that is not a subdirectory of the current directory, specify the full path to the directory as an argument to the cd
command.
cd Documents
cd /home/user/Documents
pwd
The pwd
command is used to print the current working directory.
pwd
mkdir
The mkdir
command is used to create a new directory. To create a directory that is a subdirectory of the current directory, specify the name of the subdirectory as an argument to the mkdir
command. To create a directory that is not a subdirectory of the current directory, specify the full path to the directory as an argument to the mkdir
command.
mkdir Documents
mkdir /home/user/Documents
rmdir
The rmdir
command is used to remove a directory. To remove a directory that is a subdirectory of the current directory, specify the name of the subdirectory as an argument to the rmdir
command. To remove a directory that is not a subdirectory of the current directory, specify the full path to the directory as an argument to the rmdir
command.
rmdir Documents
rmdir /home/user/Documents
touch
The touch
command is used to create a new file. To create a file that is a subdirectory of the current directory, specify the name of the file as an argument to the touch
command. To create a file that is not a subdirectory of the current directory, specify the full path to the file as an argument to the touch
command.
touch file.txt
touch /home/user/file.txt
rm
The rm
command is used to remove a file. To remove a file that is a subdirectory of the current directory, specify the name of the file as an argument to the rm
command. To remove a file that is not a subdirectory of the current directory, specify the full path to the file as an argument to the rm
command.
rm file.txt
rm /home/user/file.txt
cp
The cp
command is used to copy a file. To copy a file that is a subdirectory of the current directory, specify the name of the file as the first argument to the cp
command and the name of the new file as the second argument to the cp
command. To copy a file that is not a subdirectory of the current directory, specify the full path to the file as the first argument to the cp
command and the full path to the new file as the second argument to the cp
command.
cp file.txt newfile.txt
cp /home/user/file.txt /home/user/newfile.txt
mv
The mv
command is used to move a file. To move a file that is a subdirectory of the current directory, specify the name of the file as the first argument to the mv
command and the name of the new file as the second argument to the mv
command. To move a file that is not a subdirectory of the current directory, specify the full path to the file as the first argument to the mv
command and the full path to the new file as the second argument to the mv
command.
mv file.txt newfile.txt
mv /home/user/file.txt /home/user/newfile.txt
cat
The cat
command is used to print the contents of a file. To print the contents of a file that is a subdirectory of the current directory, specify the name of the file as an argument to the cat
command. To print the contents of a file that is not a subdirectory of the current directory, specify the full path to the file as an argument to the cat
command.
cat file.txt
cat /home/user/file.txt
less
The less
command is used to view the contents of a file. To view the contents of a file that is a subdirectory of the current directory, specify the name of the file as an argument to the less
command. To view the contents of a file that is not a subdirectory of the current directory, specify the full path to the file as an argument to the less
command.
less file.txt
less /home/user/file.txt
head
The head
command is used to print the first few lines of a file. To print the first few lines of a file that is a subdirectory of the current directory, specify the name of the file as an argument to the head
command. To print the first few lines of a file that is not a subdirectory of the current directory, specify the full path to the file as an argument to the head
command.
head file.txt
head /home/user/file.txt
tail
The tail
command is used to print the last few lines of a file. To print the last few lines of a file that is a subdirectory of the current directory, specify the name of the file as an argument to the tail
command. To print the last few lines of a file that is not a subdirectory of the current directory, specify the full path to the file as an argument to the tail
command.
tail file.txt
tail /home/user/file.txt
grep
The grep
command is used to search for a string in a file. To search for a string in a file that is a subdirectory of the current directory, specify the string and the name of the file as arguments to the grep
command. To search for a string in a file that is not a subdirectory of the current directory, specify the string and the full path to the file as arguments to the grep
command.
grep string file.txt
grep string /home/user/file.txt
wc
The wc
command is used to count the number of lines, words, and characters in a file. To count the number of lines, words, and characters in a file that is a subdirectory of the current directory, specify the name of the file as an argument to the wc
command. To count the number of lines, words, and characters in a file that is not a subdirectory of the current directory, specify the full path to the file as an argument to the wc
command.
wc file.txt
wc /home/user/file.txt
chmod
The chmod
command is used to change the permissions of a file. To change the permissions of a file that is a subdirectory of the current directory, specify the permissions and the name of the file as arguments to the chmod
command. To change the permissions of a file that is not a subdirectory of the current directory, specify the permissions and the full path to the file as arguments to the chmod
command.
chmod 777 file.txt
chmod 777 /home/user/file.txt
chown
The chown
command is used to change the owner of a file. To change the owner of a file that is a subdirectory of the current directory, specify the owner and the name of the file as arguments to the chown
command. To change the owner of a file that is not a subdirectory of the current directory, specify the owner and the full path to the file as arguments to the chown
command.
chown user file.txt
chown user /home/user/file.txt
chgrp
The chgrp
command is used to change the group of a file. To change the group of a file that is a subdirectory of the current directory, specify the group and the name of the file as arguments to the chgrp
command. To change the group of a file that is not a subdirectory of the current directory, specify the group and the full path to the file as arguments to the chgrp
command.
chgrp group file.txt
chgrp group /home/user/file.txt
ln
The ln
command is used to create a link to a file. To create a link to a file that is a subdirectory of the current directory, specify the name of the file as the first argument to the ln
command and the name of the link as the second argument to the ln
command. To create a link to a file that is not a subdirectory of the current directory, specify the full path to the file as the first argument to the ln
command and the full path to the link as the second argument to the ln
command.
ln file.txt link.txt
ln /home/user/file.txt /home/user/link.txt
mount
The mount
command is used to mount a file system. To mount a file system that is a subdirectory of the current directory, specify the name of the file system as an argument to the mount
command. To mount a file system that is not a subdirectory of the current directory, specify the full path to the file system as an argument to the mount
command.
mount filesystem
mount /home/user/filesystem
umount
The umount
command is used to unmount a file system. To unmount a file system that is a subdirectory of the current directory, specify the name of the file system as an argument to the umount
command. To unmount a file system that is not a subdirectory of the current directory, specify the full path to the file system as an argument to the umount
command.
umount filesystem
umount /home/user/filesystem
df
The df
command is used to display the amount of free disk space. To display the amount of free disk space on a file system that is a subdirectory of the current directory, specify the name of the file system as an argument to the df
command. To display the amount of free disk space on a file system that is not a subdirectory of the current directory, specify the full path to the file system as an argument to the df
command.
df filesystem
df /home/user/filesystem
du
The du
command is used to display the amount of disk space used by a file. To display the amount of disk space used by a file that is a subdirectory of the current directory, specify the name of the file as an argument to the du
command. To display the amount of disk space used by a file that is not a subdirectory of the current directory, specify the full path to the file as an argument to the du
command.
du file.txt
du /home/user/file.txt
tar
The tar
command is used to create a tar archive. To create a tar archive of a file that is a subdirectory of the current directory, specify the name of the file as an argument to the tar
command. To create a tar archive of a file that is not a subdirectory of the current directory, specify the full path to the file as an argument to the tar
command.
tar file.txt
tar /home/user/file.txt
gzip
The gzip
command is used to compress a file. To compress a file that is a subdirectory of the current directory, specify the name of the file as an argument to the gzip
command. To compress a file that is not a subdirectory of the current directory, specify the full path to the file as an argument to the gzip
command.
gzip file.txt
gzip /home/user/file.txt
gunzip
The gunzip
command is used to decompress a file. To decompress a file that is a subdirectory of the current directory, specify the name of the file as an argument to the gunzip
command. To decompress a file that is not a subdirectory of the current directory, specify the full path to the file as an argument to the gunzip
command.
gunzip file.txt
gunzip /home/user/file.txt
zip
The zip
command is used to create a zip archive. To create a zip archive of a file that is a subdirectory of the current directory, specify the name of the file as an argument to the zip
command. To create a zip archive of a file that is not a subdirectory of the current directory, specify the full path to the file as an argument to the zip
command.
zip file.txt
zip /home/user/file.txt
unzip
The unzip
command is used to extract a zip archive. To extract a zip archive that is a subdirectory of the current directory, specify the name of the file as an argument to the unzip
command. To extract a zip archive that is not a subdirectory of the current directory, specify the full path to the file as an argument to the unzip
command.
unzip file.txt
unzip /home/user/file.txt
tar.gz
The tar.gz
command is used to create a tar archive and compress it with gzip. To create a tar archive and compress it with gzip of a file that is a subdirectory of the current directory, specify the name of the file as an argument to the tar.gz
command. To create a tar archive and compress it with gzip of a file that is not a subdirectory of the current directory, specify the full path to the file as an argument to the tar.gz
command.
tar.gz file.txt
tar.gz /home/user/file.txt
tar.gz
The tar.gz
command is used to extract a tar archive that has been compressed with gzip. To extract a tar archive that has been compressed with gzip that is a subdirectory of the current directory, specify the name of the file as an argument to the tar.gz
command. To extract a tar archive that has been compressed with gzip that is not a subdirectory of the current directory, specify the full path to the file as an argument to the tar.gz
command.
tar.gz file.txt
tar.gz /home/user/file.txt
tar.zip
The tar.zip
command is used to create a tar archive and compress it with zip. To create a tar archive and compress it with zip of a file that is a subdirectory of the current directory, specify the name of the file as an argument to the tar.zip
command. To create a tar archive and compress it with zip of a file that is not a subdirectory of the current directory, specify the full path to the file as an argument to the tar.zip
command.
tar.zip file.txt
tar.zip /home/user/file.txt