Saturday, June 03, 2023

Linux Command Line Hackery Series: Part 2



Welcome back to Linux Command Line Hackery, yes this is Part 2 and today we are going to learn some new skills. Let's rock

Let us first recap what we did in Part 1, if you are not sure what the following commands do then you should read Part 1.

mkdir myfiles                                                # make a directory (folder) with myfiles as name
cd myfiles                                                      # navigate to myfiles folder
touch file1 file2 file3                                    # create three empty files file1file2file3
ls -l                                                                   # view contents of current directory
echo This is file1 > file1                               # write a line of text to file1
cat file1                                                           # display contents of file1
echo This is another line in file1 >> file1    # append another line of text to file1
cat file1                                                          # display the modified content of file1

Command:  cp
Syntax:        cp source1 [source2 ...] destination
Function:     cp stands for copy. cp is used to copy a file from source to destination. Some important flags are mentioned below
Flags:          -r copy directories recursively
                     -f if an existing destination file cannot be opened, remove it and try  again

Let us make a copy of file1 using the new cp command:

cp file1 file1.bak

what this command is going to do is simply copy file1 to another file named file1.bak. You can name the destination file anything you want.
Say, you have to copy file1 to a different folder maybe to home directory how can we do that? well we can do that like this:

cp file /home/user/

I've used the absolute path here you can use whatever you like.
[Trick: ~ has a special meaning, it stands for logged in user's directory. You could have written previous command simply as
cp file1 ~/
and it would have done the same thing.]
Now you want to create a new directory in myfiles directory with the name backup and store all files of myfiles directory in the backup directory. Let's try it:

mkdir backup
cp file1 file2 file3 backup/

this command will copy file1 file2 file3 to backup directory.
We can copy multiple files using cp by specifying the directory to which files must be copied at the end.
We can also copy whole directory and all files and sub-directories in a directory using cp. In order to make a backup copy of myfiles directory and all of it's contents we will type:

cd ..                                           # navigate to previous directory
cp -r myfiles myfiles.bak       # recursively copy all contents of myfiles directory to myfiles.bak directory

This command will copy myfiles directory to myfiles.bak directory including all files and sub-directories

Command: mv
Syntax:       mv source1 [source2 ...] destination
Function:    mv stands for move. It is used for moving files from one place to another (cut/paste in GUI) and also for renaming the files.

If we want to rename our file1 to  file1.old in our myfiles folder we'll do the follow:

cd myfiles                                      # navigate first to myfiles folder
mv file1 file1.old

this command will rename the file1 to file1.old (it really has got so old now). Now say we want to create a new file1 file in our myfiles folder and move the file1.old file to our backup folder:

mv file1.old backup/                    # move (cut/paste) the file1.old file to backup directory
touch file1                                    # create a new file called file1
echo New file1 here > file1         # echo some content into file1

Command:  rmdir
Syntax: rmdir directory_name
Function: rmdir stands for remove directory. It is used for removing empty directories.

Let's create an empty directory in our myfiles directory called 'garbage' and then remove it using rmdir:

mkdir garbage
rmdir  garbage

Good practice keep it doing. (*_*)
But wait a second, I said empty directory! does it mean I cannot delete a directory which has contents in it (files and sub-directories) with rmdir? Yes!, you cannot do that with rmdir
So how am I gonna do that, well keep reading...

Command:  rm
Syntax:        rm FILE...
Function:     rm stands for remove. It is used to remove files and directories. Some of it's important flags are enlisted below.
Flags:          -r remove directories and their contents recursively
                     -f ignore nonexistent files and arguments, never prompt

Now let's say we want to delete the file file1.old in backup folder. Here is how we will do that:

rm backup/file1.old                # using relative path here

Boom! the file is gone. Keep in mind one thing when using rm "IT IS DESTRUCTIVE!". No I'm not yelling at you, I'm just warning you that when you use rm to delete a file it doesn't go to Trash (or Recycle Bin). Rather it is deleted and you cannot get it back (unless you use some special tools quickly). So don't try this at home. I'm just kidding but yes try it cautiously otherwise you are going to loose something important.

Did You said that we can delete directory as well with rm? Yes!, I did. You can delete a directory and all of it's contents with rm by just typing:

rm -r directory_name

Maybe we want to delete backup directory from our myfiles directory, just do this:

rm -r backup

And it is gone now.
Remember what I said about rm, use it with cautious and use rm -r more cautiously (believe me it costs a lot). -r flag will remove not just the files in directory it will also remove any sub-directories in that directory and there respective contents as well.

That is it for this article. I've said that I'll make each article short so that It can be learned quickly and remembered for longer time. I don't wanna bore you.

Related posts


  1. Hack Tools
  2. Hacker Tools List
  3. Hacker Search Tools
  4. Hack And Tools
  5. Pentest Recon Tools
  6. Bluetooth Hacking Tools Kali
  7. Hack App
  8. Hacker Tools For Pc
  9. Hacking Tools Kit
  10. Best Pentesting Tools 2018
  11. Pentest Tools For Android
  12. Hacking Tools Software
  13. Pentest Tools For Android
  14. Pentest Tools Free
  15. Hacker
  16. Pentest Tools For Android
  17. Hacker Security Tools
  18. Hacker Search Tools
  19. Wifi Hacker Tools For Windows
  20. Hack Tool Apk
  21. Hacking Tools Hardware
  22. Blackhat Hacker Tools
  23. Pentest Tools Website
  24. Hacking Tools Windows 10
  25. Pentest Tools For Mac
  26. Hacking App
  27. Hacker Tools Mac
  28. Easy Hack Tools
  29. Hacker Tool Kit
  30. Pentest Tools Kali Linux
  31. Usb Pentest Tools
  32. Wifi Hacker Tools For Windows
  33. Hack And Tools
  34. Hacker Tools Github
  35. Hacker Tools Free Download
  36. Hacking Tools For Beginners
  37. Hacker Search Tools
  38. Pentest Tools Website Vulnerability
  39. Github Hacking Tools
  40. Pentest Tools Free
  41. Hack Tools For Mac
  42. Nsa Hacker Tools
  43. Pentest Tools Github
  44. Hack Tools Download
  45. Hacker Tools Online
  46. How To Install Pentest Tools In Ubuntu
  47. What Are Hacking Tools
  48. Nsa Hack Tools Download
  49. Hacker Tools For Mac
  50. How To Install Pentest Tools In Ubuntu
  51. Pentest Tools Download
  52. Hack Tools 2019
  53. Hacker Tools Free
  54. Easy Hack Tools
  55. Hacking Tools 2019
  56. Hacker Tools For Ios
  57. Hacker Tools For Ios
  58. Hacking Tools For Windows 7
  59. Pentest Tools For Ubuntu
  60. Hackers Toolbox
  61. Hacking Tools Hardware
  62. Hack Tools For Pc
  63. Hacking Tools Windows
  64. Pentest Tools Tcp Port Scanner
  65. Hacker
  66. Hack Tools Pc
  67. Hacker Tools Windows
  68. Hack Tools 2019
  69. Hacker Tools 2019
  70. Hack Tools For Pc
  71. Pentest Box Tools Download
  72. Android Hack Tools Github
  73. Pentest Tools For Windows
  74. Hacker Tools Apk
  75. Hacking Tools 2020
  76. Pentest Tools Download
  77. Hack Tools Mac

0 Comments:

Post a Comment

<< Home