Lately, I started learning commands lines. I mean, I used npm install
or gulp
, but no more.
So I started using commands lines and I have to say that I like it. A lot.
I wanted to share with you what I learned.
cd
The cd
command is used to change the current directory.
cd stand for change directory
cd path/of/directory
To move up by one directory you can hit
cd ..
Source
ls
The ls
command list all files of a type other than directory, found in the current directory.
ls stand for list segments
ls
man
The man
command display the online manual pages. We can specify a section as parameter. The section is normally the command, function or file name.
man stand for manual.
man name_of_command
To exit the manual you can hit
q
,:q
,Q
,:Q
orZZ
.
mkdir
The mkdir
command is used to create a new directory. The directory would be create in the current directory.
mkdir stand for make directories.
mkdir name_of_directory
rmdir
The rmdir
command is used to remove an empty directory.
rmdir stand for remove directories.
rmdir name_of_directory
We can pass an -p
option. In this case each directory argument will be treated as a single path and will be removed if they are empty.
rmdir -p name/of/directory
mv
The mv
command is used to move a file into a given directory.
mv stand for move.
mv name_of_file name/of/directory/destination/
open
The open
command is used to open a file or a directory
open name_of_file
I know, it's very basic, but you have to start somewhere. 😅
I created a Gist where I add what I learn.
And you, what are your favorite commands?
Latest comments (30)
how to delete all the records in the file at a time retaining the file / folder
I'd like to recommend this NPM module: npmjs.com/package/tldr
It's basically like the 'man' command, but usually with a shorter description and example uses. For example, 'tldr tar' will tell you which flag combination you need to extract different types of compressed archives.
And I'll add another few commands that I use daily:
| When you want to delete a non-empty directory, use with caution!ls -a | Basically like ls, but also displays hidden files.
rm -rf
mkdir dir/{subdir1,subdir2} | Use curly braces to create multiple subdirs at once.
cd | Just typing cd is the same as 'cd ~', which brings you to your home directory.
cat | Will print to contents of a file to the console, so you don't have to open the file just to view it.
Also, check out your .bashrc by typing cat ~/.bashrc, in there you'll find several bash aliases, as well as having the possibility to create your own.
And again, don't use rm -rf, if you're not 100% certain that you know what you're doing.
If you want to check a file quickly without having to deal with
vim/emacs/nano
and it's too long to usecat/echo
,more
is really useful but it only reads in one direction. For both directions check outless
to read both directionsGreat post Jérémy!
Some more:
ps aux
will show you the running processes.netstat
will show you the open network ports.Both of these will have a very large output, so combine with
grep
to filter for a specific string:$ ps aux | grep mysql
is a good way to see what process represents a MySQL instance, for example.NOTE: the
$
is conventionally used to represent the start of a command you type into your bash terminal and run withreturn
orenter
. You don't type out the$
character.This is useful when your application is not running as expected and you want to check than any requried processes have been started.
You can use this to figure out what is running on a particular port (and kill it if desired):
stackoverflow.com/questions/115835...
I end up using these a lot when working with introductory web development students -- they don't know how to check what is running, end up starting 2 or more instances of a server process, and then get confused when they can't connect to their web application.
You mentioned the -p flag for rmdir. It can also be used with mkdir, as 'mkdir x/y/z'.
As a few people have mentioned, you can add aliases in your ~/.bashrc.
My favorite is:
mkdcd() {
mkdir $1
cd $1
}
Which makes creating and changing your cwd easy.
And also: cd.. for cd ..
Some time ago, I build a .sh where I put all my little scripts to semi-automitize some task: open certain programs with my configs, create my projects with git and makefile, update/upgrade my system, compress files... Other more complex to remember, like connecting to an external monitor via VGA/HDMI. 15 or 20 lil'scripts to make my life easier. Why? If there is a easiest way to do something, I will choose it.
Awesome start Jeremy. When I played with Unix terminal for the first time, I made a major mistake which was not going over the basics.
Add these especially if you are in a directory
cd ..
takes you backcd -
takes you to the previous directoryls -sh
lists the files/directories with their sizesls -l
lists the details including permissions.There are lots more, but add these to yours
Have fun in your journey!
grep
is awesome way to search for stuff.Particularly when combined with
|
operator to chain stuff.For example,
ls | grep my_file.txt
save yourself.
unlearn rmdir.
now.
There's something wrong with
rmdir
?nope, nothing at all
my bad
I saw "rmdir" and read "rm -rf"
rm -rf
does incredible and irreparable damage except you have backups handy.You can't restore a backup if you
rm -rf /
.Exactly. You just wiped out the entire system..
Well except if you have an external backup of some sort. 😏
The find command is one of the most useful on your toolkit
find . -exec cp {} . \;
(Recusevely find all files in a path and copy to the root)
Although you should really be using fd