Header Ads

(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.
(Linux Command Line) Part 2 - Linux Commands for File and Folder/Directory Management

Serial Linux Commands:

Bonus: E-book "Total Linux Command Line", Download Here

Linux Commands for File and Folder/ Directory Management

1. View File and Folder details

LINUX COMMANDPURPOSE / DESCRIPTION
  lsDisplay all files and folders in the current folder
  ls foldernameList the content in the foldername
  ls -lList the content in the current folder, includes: size, last updated time...
  ls -aList the content in the current folder, includes hidden files
  pwdProvide the path of working folder
  cdChange working folder (like the command CD in MS-DOS)
  dfCheck disk space
  duCheck 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 COMMANDPURPOSE / DESCRIPTION
  mountMount/ 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.
  umountUnmount file system from a storage device
  mkdirMaKe DIRectory: Create a new folder, eg: mkdir Folder-Name
  touchCreate a new and empty file.
  rmdirRemove/ Delete an empty folder(s) (to delete a folder with content (not empty, use this command: rm -rf )
  mvRename or Move file/folder
  rmRemove/Delete file(s)
  cpCopy file(s) to another location eg: cp source-file.txt des-file.txt
  chmodSet file/ folder permission. Format: chmod right-code file/folder-name
  wgetDownload a file from a URL
  findFind 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
  lprPrint a file
  lprmCancel printing queues
  lpqDisplay the list of printer queues
  echo strCopy str to the command window

3. Linux Commands for File Editor

LINUX COMMANDPURPOSE / DESCRIPTION
  wcCount the number of lines, words and characters in the text
  catDisplay file content
  moreView file content by page
  lessView file content by line
  viOpen 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
  headReads the first few lines (10 by default). To display the first 100 line, use the command: head 100 file-name
  ln existingfile new-linkCreate a path to a file (hard links)

4. Linux Command for Compression and Decompression

LINUX COMMANDPURPOSE / DESCRIPTION
  tar  -cvfCompress files/folders with.TAR format
  tar  -xvfDecompress a .TAR file
  gzipConvert .TAR file to .TAR.GZ file
  gunzipConvert .TAR.GZ file to .TAR file
  tar -xzfDecompress a .TAR.GZ file, eg: tar -xvf archive.tar
  tar -zxvfDecompress a .tar.bz2 file
  tar -jxvfDecompress a .tar.gz2 file
  tar -cvzpfCompress a folder, eg: tar -cvzpf archive.tgz /home/example/public_html/folder
  unzipDecompress 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.

No comments:

EasyTipz. Powered by Blogger.