DEV Community

Cover image for Don't fear the command line: Manipulation
Dionysia Lemonaki
Dionysia Lemonaki

Posted on

Don't fear the command line: Manipulation

On my quest to stop being intimidated by the command line, the more I learn about it, the more fascinated I become by it.

In my last post ,which you can read here, I talked about the absolute basics anyone needs to get started using the terminal. But there is so much more you can do and in this post I'm going to dive deeper into not only viewing your filesystem in more detail but actually making changes to it.

You got options

We use ls to list the contents of the current directory, however , we can use ls with options which are usually preceded by the - character .Options modify and change the default behaviours of the commands they are paired with. Let me explain:

  • ls -a lists all contents of a directory, including hidden files and directories. Files starting with a dot . are hidden and are not displayed with using ls alone.
  • ls -l lists all contents of a directory in long format and displayed as a table. Each column represents access rights, hard links( number of child directories and files), the username ,the size of the file in bytes, the date and time the files were last modifies and the file's name.
  • ls -t orders files and directories by the date and time they were last modified.

Each option can be used separately or multiple options can be used together like -alt . In this case -alt lists all files and directories, including hidden ones in long format, ordered by the date and time they were last modified.

I like options


We can also use the command line to copy, move and remove files and directories

Copy

  • cp copies files and directories. To copy a file into a directory we use cp with the source file as the first argument and the destination directory as the second argument. If we have the file codingbooks.txt that's inside the Books directory and we want to copy it's contents into the Coding directory, then we would do :
 cp Books/codingbooks.txt Coding
Enter fullscreen mode Exit fullscreen mode

To copy multiple files into a directory use cp with a list of the files sources as the first arguments and the destination directory as the last argument:

cp Books/codingbooks.txt Books/ruby.txt Coding
Enter fullscreen mode Exit fullscreen mode

Move

  • mv works in the exact same way as cp does. We use the file we want to move as the first argument and the destination directory as the second argument. In this case however, we can use mv to also rename files . Say we want to rename notes.txt to Notes.txt because we changed our mind and want for files to start with capital letters:
mv notes.txt Notes.txt
Enter fullscreen mode Exit fullscreen mode

happy dance

Remove

  • rm removes files. In order to remove directories we use rm -r . The -r is an option that stands for recursive .It modifies the command, similar to what we talked about earlier on, and deletes the directory and child directories. For example,to delete the Pictures directory:
rm -r Pictures
Enter fullscreen mode Exit fullscreen mode

Be careful when using these commands because once you delete the files or directories, they are permanently deleted! You will not be able to retrieve them from the Bin in the GUI.
it's gone

Wildcards

This one is the most powerful one in my humble opinion and the one that stood out to me the most. Using * we can select multiple groups of files and directories. Instead of spending lot's of time dragging and dropping files in the GUI or typing in the terminal, if we know we want to select many files of a certain type or that are located in a certain directory, we can use this instead. Below I have a few examples of it's use :

  • To list all files in the Documents directory, in long format, that end in .pdf
ls -l Documents/*.pdf
Enter fullscreen mode Exit fullscreen mode
  • To copy all files that begin with m and end with .txt in the current directory, to the Documents directory
cp m*.txt Documents
Enter fullscreen mode Exit fullscreen mode
  • To move all files in the working directory to the Pictures directory
mv * Pictures
Enter fullscreen mode Exit fullscreen mode
  • To delete all files in the Downloads directory
rm Downloads/*
Enter fullscreen mode Exit fullscreen mode

Those are some of the ways we can manipulate and alter the file system using the terminal.
If you have made it this far, you deserve a cat gif
cat

Discussion (16)

Collapse
schlenges profile image
Meike

This was such a great read! Thank you so much! I even learned something new!! πŸ˜ƒ
I really enjoy the way you write, Dionysia! It's such a pleasure to read, your gifs always make me grin and your explanations are so clear and easy to understand - I wish I had these articles when I first started out getting familiar with the command line! :) I can't wait to read more from you! 😊

Collapse
deniselemonaki profile image
Dionysia Lemonaki Author

Your comment made me day :)
Thank you so much for your support and the nice words you said to me. You are one of the kindest people I have come across.
Thanks again :)

Collapse
schlenges profile image
Meike

Oh, of course! I absolutely mean it, it's always such a joy to see people being so genuinely interested and excited about code and tech and everything that comes along with it! I'm so glad that you decided to invest the time in writing out and sharing all your findings, so again, if anything, I thank you! πŸ˜…πŸ˜Š I'm just stoked to -alt list everything from now on! πŸ˜„

Thread Thread
deniselemonaki profile image
Dionysia Lemonaki Author

This makes me so happy, yes -alt on everything from now on haha :)

Collapse
waylonwalker profile image
Waylon Walker • Edited on

#meta DEV tip

In my last post ,which you can read here, I talked about the absolute basics

using liquid tags to promote your previous posts gives it a bit more visual flair and breaks up the document a bit.

{% post deniselemonaki/don-t-fear-the-command-line-navigation-57od %}
Collapse
deniselemonaki profile image
Dionysia Lemonaki Author

Thank you for that tip!

Collapse
designeranna1 profile image
Designer Anna • Edited on

Thanks for this small revision course of command line commands.
The best one I like is the ' rm ' command, which can be used both for moving and renaming files.

Also, I just realised that I cannot empty bin folder with that command.
Keep Coding, keep growing.!

Collapse
deniselemonaki profile image
Dionysia Lemonaki Author

Thank you so much, glad you enjoyed it! :)

Collapse
waylonwalker profile image
Waylon Walker

Learning the command and being comfortable with it is a super-power. There are things that seem easier and more intuitive with a GUI for most of us. But once you get more comfortable you start to find ways that you can script it, which makes you feel like a wizard. You can also start setting up aliases that do things that you do often with very few keystrokes. At some point, the GUI starts to feel a bit barbaric in comparison.

Collapse
farid_aditya profile image
Farid Aditya

thank you

Collapse
mungojam profile image
Mark Adamson

I never knew about -t, and I've been using Unix on and off for years. very handy, thanks!

Collapse
deniselemonaki profile image
Dionysia Lemonaki Author

Thank you so much, I appreciate that!

Collapse
adrianmarkperea profile image
Adrian Perea

Very great for those starting out!

Collapse
deniselemonaki profile image
Dionysia Lemonaki Author

Thank you so much!

Collapse
deniselemonaki profile image
Dionysia Lemonaki Author

I'll give it a read :)