Hey everyone! π
If you're a developer, you know that the command line can be your best friend. It's powerful, it's fast, and it can make you feel like a wizard when you know what you're doing. But if you're just starting out, that blinking cursor can be a little intimidating.
Fear not! In this guide, we're going to break down the essential commands that every developer should have in their toolkit. We'll cover everything from navigating your file system to managing processes, so you can spend less time fumbling around and more time building amazing things.
Let's dive in!
Navigating Your File System π
First things first, let's get you moving around your computer like a pro. These commands are the absolute basics for getting from point A to point B.
pwd (Print Working Directory): Ever get lost in your own computer? It happens to the best of us. Just type pwd to see exactly where you are in the file system.
ls (List): This is your go-to for seeing what's inside a directory. Just type ls to get a list of all the files and folders in your current location.
ls -l: Want more details? The -l flag gives you a "long list" with permissions, owner, size, and when the file was last modified.
ls -a: Need to see everything, including hidden files? The -a flag is your friend. You can even combine them like ls -al to get all the details on all the files.
cd (Change Directory): This is how you move around. Type cd followed by the name of a directory to go into it.
cd ..: Need to go back up a level? cd .. will take you to the parent directory.
cd ~: Want to go straight to your home directory? cd ~ is a handy shortcut.
Working with Files and Directories π
Now that you can navigate, let's start creating and managing files and directories.
touch: This is a quick way to create a new, empty file. If the file already exists, touch will just update its timestamp.
mkdir (Make Directory): Need a new folder? mkdir followed by the folder name will create it for you.
cp (Copy): To copy a file, use cp followed by the source file and the destination. To copy a whole directory and everything in it, use the -r (recursive) flag.
mv (Move): You can use mv to move a file to a different directory, or to rename a file. For example, mv old-name.txt new-name.txt will rename the file.
rm (Remove): Be careful with this one! rm will delete a file. To delete a directory and everything inside it, you'll need the -r flag.
rm -rf /: NEVER EVER RUN THIS COMMAND! This will try to delete everything on your computer. You've been warned!
Viewing and Editing Files π
Okay, you've got files, now how do you look at them?
cat: This command will display the entire contents of a file right in your terminal.
head and tail: For big files, you might not want to see everything at once. head will show you the first 10 lines, and tail will show you the last 10. You can use the -n flag to specify a different number of lines (e.g., head -n 5 my-file.txt).
less and more: These are great for viewing large files. They'll open the file in a "pager," so you can scroll through it line by line or page by page. less is a bit more modern and has more features.
nvim (Neovim): When you need to actually edit a file, a text editor like Neovim is a great choice. You can open a file with nvim my-file.txt, press i to go into "insert mode" to type, Esc to go back to "normal mode," :w to save, and :q to quit.
Searching and Finding π
Lost a file? Or need to find a specific line of code? These commands have your back.
find: This command is a powerful way to search for files. You can find files by name, type, size, and more. For example, find . -name "*.js" will find all the JavaScript files in the current directory and its subdirectories.
grep: This is one of the most useful commands out there. It searches for a pattern of text within files. For example, grep "function" my-script.js will show you all the lines in that file that contain the word "function".
grep -r: The -r flag will search recursively, so you can search through all the files in a directory.
grep -n: Use -n to show the line numbers of the matches.
grep -i: The -i flag makes your search case-insensitive.
grep -v: This inverts the match, showing you all the lines that don't contain your search term.
System and Process Management βοΈ
Time to take a look under the hood and see what's going on with your system.
whoami: A simple one, but useful. This tells you the username of the account you're currently logged into.
sudo (Super User Do): Some commands need to be run with administrative privileges. sudo lets you do that. It will usually ask for your password.
history: Want to see the commands you've run recently? history will give you a list.
clear: If your terminal is getting cluttered, clear will wipe it clean and give you a fresh start.
chown and chmod: These are for managing file permissions. chown changes the owner of a file, and chmod changes the permissions (who can read, write, and execute the file).
ps aux: This command will show you a list of all the processes currently running on your system.
kill: If a program is misbehaving and you need to shut it down, kill followed by the process ID (which you can get from ps aux) will do the trick.
top: This gives you a real-time view of your system's processes, so you can see what's using up your CPU and memory.
man (Manual): Don't know how to use a command? man followed by the command name will show you the manual page with all the details.
Package Management and Networking π¦
apt and brew: These are package managers for Linux and macOS, respectively. They make it super easy to install, update, and remove software. For example, sudo apt install neovim or brew install neovim will install the Neovim text editor.
curl: This is a command-line tool for making network requests. You can use it to download files, test APIs, and more.
And a Few More...
echo: This command simply prints text to the terminal. It's often used in shell scripts.
wc (Word Count): This command can count the number of lines, words, and characters in a file.
lsd: This is a modern, more colorful version of the ls command. It's a great example of how the command line is always evolving!
And that's a wrap! We've covered a lot of ground, but don't feel like you need to memorize everything at once. The best way to learn is by doing. So open up your terminal, start playing around with these commands, and see what you can do.
Happy coding! π
Top comments (0)