(Linux Command Line) Part 2 - Linux Commands for File and Folder/ Directory Management
In this part of serial Linux Commands, we will learn using Linux Commands for File and Folder/ Directory Management. File and Folder management includes these main tasks: disk and partition management, create file/ folder, edit file content, rename file/ folder, set permission for user(s), move file/ folder.
Remember that Commands in CentOS and Ubuntu are very similar, but sometimes there are differences.
Serial Linux Commands:
>> Part 1 – Linux Commands for Server Management and Monitoring
>> Part 2 – Linux Commands for File and Folder/ Directory Management
>> Part 3 – Linux Commands for File Editor in Linux (CentOS, Ubuntu)
>> Part 4 – Linux Commands for Network Configuration in Linux (CentOS, Ubuntu)
>> Part 5 – Other Useful Linux Commands (CentOS, Ubuntu) you need to know
Linux Commands for File and Folder/ Directory Management
1. View File and Folder details
LINUX COMMAND | PURPOSE / DESCRIPTION |
ls | Display all files and folders in the current folder |
ls foldername | List the content in the foldername |
ls -l | List the content in the current folder, includes: size, last updated time… |
ls -a | List the content in the current folder, includes hidden files |
pwd | Provide the path of working folder |
cd | Change working folder (like the command CD in MS-DOS) |
df | Check disk space |
du | Check Disk Usage: Sizes of folder trees inclusive of all of their contents and the sizes of individual files |
2. Linux Commands for File and Folder activities (create, delete/ remove, search…)
LINUX COMMAND | PURPOSE / DESCRIPTION |
mount | Mount/ Attach file system from a storage device (other device, such as a CDROM, DVD, floppy disk or USB key drive) – mount /mnt/floppy: Attach a floppy disk – umount /mnt/floppy: Detach a floppy disk – mount /mnt/cdrom: Attach a CD-ROM. |
umount | Unmount file system from a storage device |
mkdir | MaKe DIRectory: Create a new folder, eg: mkdir Folder-Name |
touch | Create a new and empty file. |
rmdir | Remove/ Delete an empty folder(s) (to delete a folder with content (not empty, use this command: rm -rf ) |
mv | Rename or Move file/folder |
rm | Remove/Delete file(s) |
cp | Copy file(s) to another location eg: cp source-file.txt des-file.txt |
chmod | Set file/ folder permission. Format: chmod right-code file/folder-name |
wget | Download a file from a URL |
find | Find file or content in file, eg: – find /etc -name inittab: Find all file in the folder /etc that its name contains inittab – grep text filename: Search for string text in filename |
lpr | Print a file |
lprm | Cancel printing queues |
lpq | Display the list of printer queues |
echo str | Copy str to the command window |
3. Linux Commands for File Editor
LINUX COMMAND | PURPOSE / DESCRIPTION |
wc | Count the number of lines, words and characters in the text |
cat | Display file content |
more | View file content by page |
less | View file content by line |
vi | Open a file with VI editor |
tail | Reads the final few lines (10 by default). To display the final 100 line, use the command: tail 100 file-name |
head | Reads the first few lines (10 by default). To display the first 100 line, use the command: head 100 file-name |
ln existingfile new-link | Create a path to a file (hard links) |
4. Linux Command for Compression and Decompression
LINUX COMMAND | PURPOSE / DESCRIPTION |
tar -cvf | Compress files/folders with.TAR format |
tar -xvf | Decompress a .TAR file |
gzip | Convert .TAR file to .TAR.GZ file |
gunzip | Convert .TAR.GZ file to .TAR file |
tar -xzf | Decompress a .TAR.GZ file, eg: tar -xvf archive.tar |
tar -zxvf | Decompress a .tar.bz2 file |
tar -jxvf | Decompress a .tar.gz2 file |
tar -cvzpf | Compress a folder, eg: tar -cvzpf archive.tgz /home/example/public_html/folder |
unzip | Decompress a .zip file |
How to use Linux Commands for File and Folder Managment
Edit file with VI text Editor
For example, we will try to edit file PHP.INI using VI editor. Firstly, type this command:
vi /etc/php.ini
– Press key Insert to enter “Editing mode” then edit the file content.
– When you are finished, press Insert key once more time to exit “Editing mode“.
– Finally, save the file with below command:
:wq
And here are other commands for VI editor:
:w -Save the file but does not exit VI editor
: x – Save the file and exit VI editor
:wq – Save the file and exit VI editor
:w – Save the content into a new file
:q – Exit VI editor if there is no change
:q! – Exit VI editor
:r – Read a file
Create a new Partition in Linux
– Firstly, create a new folder in /mnt with the below command:
mkdir /mnt/New-Partition
– Then, use MOUNT command:
mount /dev/source /mnt/New-Partition
(/dev/source is the storage device (partition) we want to attach to the file system)
Find/ Search for file/ file content in Linux
– To search for file by filename, use the command: FIND
– To search for a string (text) in file content, use the command GREP:
grep < keyword/ condition>
– With a long content, we can use the command MORE to display text by page
– To get all file content, use the command: CAT
cat < file name>
– To View only some content in the top or bottom of a file, use the command HEAD or TAIL.
Cheers!
If you need any further help, feel free to contact us by leaving a comment on each article.
Don’t forget to follow us on social channels to get latest tips.
Source: https://easytipz.com/computer-tips/linux-command-line-part-2-commands-for-file-and-folder-directory-management/
No comments: