DEV Community

Big Mazzy
Big Mazzy

Posted on • Originally published at serverrental.store

Essential Linux Commands Every Developer Should Know

Ever wondered how to efficiently navigate and manage your development environment on Linux? Mastering a few core commands can significantly boost your productivity and confidence. This article will walk you through some essential Linux commands that every developer should have in their toolkit, focusing on practical applications for daily tasks.

Navigating the File System: cd, ls, and pwd

Understanding how to move around your file system is fundamental. These commands allow you to see where you are and where you can go.

pwd: Where Am I?

The pwd (print working directory) command tells you your current location in the file system hierarchy. This is like asking "Where am I?" when you're lost in a maze.

pwd
Enter fullscreen mode Exit fullscreen mode

This will output the absolute path of your current directory, for example, /home/developer/projects.

ls: What's Here?

The ls command lists the contents of a directory. You can use various flags to customize its output, making it incredibly versatile.

ls
Enter fullscreen mode Exit fullscreen mode

This will show you files and directories in your current location.

ls -l
Enter fullscreen mode Exit fullscreen mode

The -l flag provides a long listing format, showing permissions, owner, size, and modification date.

ls -a
Enter fullscreen mode Exit fullscreen mode

The -a flag shows all files, including hidden ones (those starting with a dot, like .bashrc).

cd: Let's Go Somewhere Else

The cd (change directory) command allows you to move between directories.

cd /path/to/your/directory
Enter fullscreen mode Exit fullscreen mode

This takes you to the specified directory.

cd ..
Enter fullscreen mode Exit fullscreen mode

This moves you up one directory level.

cd ~
Enter fullscreen mode Exit fullscreen mode

This takes you back to your home directory.

For developers increasingly working with remote servers, efficient navigation is key. Providers like PowerVPS offer reliable and fast Linux VPS instances perfect for hosting your projects and practicing these commands.

File and Directory Management: mkdir, touch, cp, mv, rm

Once you can navigate, you'll need to create, copy, move, and delete files and directories.

mkdir: Making New Spaces

The mkdir (make directory) command creates new directories.

mkdir new_project
Enter fullscreen mode Exit fullscreen mode

This creates a directory named new_project in your current location. You can also create nested directories with the -p flag.

mkdir -p projects/web/frontend
Enter fullscreen mode Exit fullscreen mode

This creates projects, then web inside projects, and finally frontend inside web.

touch: Creating Empty Files

The touch command creates new, empty files. If the file already exists, it updates its timestamp.

touch README.md
Enter fullscreen mode Exit fullscreen mode

This creates a file named README.md.

cp: Making Copies

The cp (copy) command copies files or directories.

cp source_file.txt destination_file.txt
Enter fullscreen mode Exit fullscreen mode

This copies source_file.txt to destination_file.txt. To copy directories, use the -r (recursive) flag.

cp -r source_directory destination_directory
Enter fullscreen mode Exit fullscreen mode

mv: Moving and Renaming

The mv (move) command is used to move files or directories, or to rename them.

mv old_name.txt new_name.txt
Enter fullscreen mode Exit fullscreen mode

This renames old_name.txt to new_name.txt.

mv my_file.txt /path/to/new/location/
Enter fullscreen mode Exit fullscreen mode

This moves my_file.txt to the specified new location.

rm: Deleting Things

The rm (remove) command deletes files. Be extremely careful with this command, as deleted files are usually unrecoverable.

rm my_file.txt
Enter fullscreen mode Exit fullscreen mode

This deletes my_file.txt. To remove directories, use the -r flag.

rm -r my_directory
Enter fullscreen mode Exit fullscreen mode

The -f (force) flag can be used to override prompts, but use it with extreme caution. rm -rf / is a command that can delete your entire system.

Viewing and Editing Files: cat, less, head, tail, nano

You'll frequently need to view the contents of files or make quick edits.

cat: Concatenate and Display

The cat (concatenate) command is often used to display the contents of a file. It can also be used to join multiple files.

cat my_log_file.log
Enter fullscreen mode Exit fullscreen mode

This prints the entire content of my_log_file.log to your terminal.

less: Paging Through Files

For larger files, cat can be overwhelming. less is a pager that lets you scroll through files page by page.

less large_file.txt
Enter fullscreen mode Exit fullscreen mode

Use arrow keys to navigate, Page Up/Page Down to scroll, and q to quit.

head and tail: Peeking at the Edges

head displays the beginning of a file, and tail displays the end. This is incredibly useful for checking logs.

head my_log_file.log
Enter fullscreen mode Exit fullscreen mode

By default, shows the first 10 lines.

tail my_log_file.log
Enter fullscreen mode Exit fullscreen mode

By default, shows the last 10 lines.

tail -f my_log_file.log
Enter fullscreen mode Exit fullscreen mode

The -f flag (follow) is a lifesaver for real-time log monitoring. It will continuously display new lines as they are added to the file. This is invaluable when debugging live applications.

nano: Simple Text Editing

While more powerful editors exist, nano is a straightforward command-line text editor that's easy for beginners.

nano my_config.conf
Enter fullscreen mode Exit fullscreen mode

This opens my_config.conf in nano. You can edit the text and save by pressing Ctrl+X, then Y to confirm, and Enter to accept the filename.

For developers who might need to manage multiple servers for testing or deployment, services like Immers Cloud offer flexible cloud solutions that can be provisioned quickly.

Searching and Filtering: grep and find

Finding specific information within your files or system is a common development task.

grep: Pattern Matching Powerhouse

grep (global regular expression print) searches for lines that match a pattern in files.

grep "error" my_log_file.log
Enter fullscreen mode Exit fullscreen mode

This will display all lines in my_log_file.log that contain the word "error".

grep -i "warning" production.log
Enter fullscreen mode Exit fullscreen mode

The -i flag makes the search case-insensitive.

grep -r "function_name" /path/to/project
Enter fullscreen mode Exit fullscreen mode

The -r flag makes grep search recursively through directories. This is excellent for finding where a specific piece of code is used across your codebase.

find: Locating Files

The find command searches for files and directories within a specified path based on various criteria.

find . -name "*.js"
Enter fullscreen mode Exit fullscreen mode

This searches the current directory (.) and its subdirectories for all files ending with .js.

find /var/log -type f -mtime +7
Enter fullscreen mode Exit fullscreen mode

This finds all regular files (-type f) in /var/log that were modified more than 7 days ago (-mtime +7). This is useful for cleaning up old log files.

A great resource for understanding server options and making informed decisions about where to host your applications is the Server Rental Guide.

Permissions and Ownership: chmod and chown

Understanding file permissions is crucial for security and proper application functioning.

chmod: Changing Permissions

The chmod command changes the access permissions of files and directories. Permissions are read (r), write (w), and execute (x). They can be set for the owner, the group, and others.

chmod +x my_script.sh
Enter fullscreen mode Exit fullscreen mode

This adds execute permission for the owner, group, and others to my_script.sh, making it runnable.

You can also use numeric modes:

  • 7 (rwx)
  • 6 (rw-)
  • 5 (r-x)
  • 4 (r--)
chmod 755 my_script.sh
Enter fullscreen mode Exit fullscreen mode

This sets permissions to rwxr-xr-x (owner can read, write, execute; group and others can read and execute).

chown: Changing Ownership

The chown command changes the owner and/or group of files and directories.

chown developer:developers my_file.txt
Enter fullscreen mode Exit fullscreen mode

This changes the owner of my_file.txt to developer and the group to developers.

Conclusion

These essential Linux commands form the bedrock of effective command-line usage for developers. From navigating your file system to searching for code and managing file permissions, these tools will streamline your workflow. Regular practice, especially on a development server or VPS, will solidify your understanding and make you a more capable and efficient developer. Experimenting with these commands on a reliable hosting platform like PowerVPS or Immers Cloud is a fantastic way to build confidence.


Disclosure: This article contains affiliate links to PowerVPS and Immers Cloud. If you click through and make a purchase, I may receive a commission at no additional cost to you. This helps support my work.

Top comments (0)