Nikola Brežnjak blog - Tackling software development with a dose of humor
  • Home
  • Daily Thoughts
  • Ionic
  • Stack Overflow
  • Books
  • About me
Home
Daily Thoughts
Ionic
Stack Overflow
Books
About me
  • Home
  • Daily Thoughts
  • Ionic
  • Stack Overflow
  • Books
  • About me
Nikola Brežnjak blog - Tackling software development with a dose of humor
Linux

50+ commands ChatGPT thinks you should know about Terminal aka Command Line

TL;DR

Hey there and happy Wednesday* 👋

*a day on which, historically, most people wed on; thus: Wed nes day
Not really, but it would be a fun fact actually 🙂

OK, back on point; in this post, I’m going to take you through the basics of using the Terminal aka (as the cool kids using Windows call it) the Command Line. We’ll cover some basic commands, why it’s super useful, and how you can start flexing your command line muscles in no time!

Reality Check

Nowadays, on your first day at a new tech job, you probably aren’t going to be asked to ‘just SSH into the server and deploy the latest build.’. However, that doesn’t mean that knowing the basics of moving around the Linux system is something you shouldn’t learn. If nothing else, it’ll give you additional knowledge; and that’s why you came into this industry, right?

Great, then let’s go 💪

Why Terminal, You Ask?

Yes, the command line can seem intimidating with its blinking cursor and lack of buttons, but it’s really not that scary. Think of it as a conversation with your computer – you just need to know the right words.

Installing the Terminal 😏

Usually, you don’t need to install it; it comes with your operating system. But if you’re on Windows, you might want to look into installing something like Git Bash or PowerShell for a better experience. Or, better yet (if you don’t have a Linux server handy), try it out in a sandbox…

Experimenting in the Sandbox

The best way to learn is by doing. Don’t be afraid to experiment with commands.

The worst that can happen? You might delete your whole system 🙂. So, I suggest using something like https://cocalc.com/ (there are many more options like this, Google them) to test it out in a so-called sandbox environment.

Essentials

1. Opening the Terminal

First things first. To start using the Terminal, you need to open it. On macOS, you’ll find it in Applications > Utilities. Windows folks, look for Command Prompt or PowerShell`. Linux users, you probably already know what you’re doing 🙂

2. The Basic Commands

  • pwd (Print Working Directory)
    • This command tells you where you are. It’s like asking, "Hey Terminal, where the heck am I in this file system maze?"
    • You’ll get an output something like: /home/nikola
  • ls (List)
    • Want to know what files are lurking in the current directory? Just type ls. It’s like peeking into a room to see what’s inside.
  • cd (Change Directory)
    • Need to move around? Use cd followed by the directory’s path. It’s your ‘teleport’ command.
    • If you press ENTER after just the cd command, it will place you in your home directory (/home/nikola in my case)
  • mkdir (Make Directory)
    • To create a new folder, use mkdir followed by the name you want to give it. It’s like conjuring a box out of thin air.
    • For example: mkdir newFolder

3. Editing and Viewing Files

  • touch
    • Create an empty file; for example: touch file.md
  • nano, vim, or emacs
    • These are text editors available right in the Terminal. It might not be as fancy as your VS Code, but it gets the job done. For example, to open the file file.md type nano file.md
    • If you’re really just starting out, go with nano, or be lost in vim forever
    • Jokes aside, there’s a certain level of satisfaction and productivity among folks that master vim
  • cat (Concatenate)
    • Want to quickly view the contents of a file? cat followed by the file name will display it for you. No need to open it in an editor.
    • Example: cat file.md

4. File Manipulation

  • cp (Copy)
    • Use this to copy files or directories.
    • Example: cp file.md file-copy.md
  • mv (Move)
    • Use this to move or rename files or directories.
    • Example: cp file.md file2.md
  • rm (Remove)
    • ⚠️ Be careful with this one as it deletes files or directories. There’s no going back, so use it wisely!

5. Getting Help

  • man (Manual)
    • Stuck with what a command does? Type man followed by the command, and you’ll get a detailed manual. It’s like asking for directions.
    • Example: man ls will give you all the info about the ls command

6. The Power Moves

  • echo
    • output to the console whatever you put in quotes
    • Example: echo "testing" will output ‘testing’ to the console output
  • >
    • redirect the output of a command to another file or program
    • Example: echo "testing" > file.md will overwrite the file.md with the word ‘testing’
    • If you want to append, instead of overwrite, use >>
  • grep
    • This command lets you search through files for specific text. It’s like having a searchlight.
    • Example: grep 'testing' file.md
  • | (Pipe)
    • This symbol is used to take the output of one command and use it as the input for another. It’s like connecting Lego blocks to build something cool.
    • Example: ls | grep "myFile"
    • this command sequence will list all files and directories, but only display those whose names include "myFile".

7. SSH and Remote Servers

  • ssh (Secure Shell)
    • This is how you remotely log into another machine. It’s like teleporting to a different computer.
    • Example: ssh -p port_number username@remote_host

8. Understanding File Paths

File paths can be tricky for beginners. Remember, there are absolute paths (which start from the root directory aka /) and relative paths (which start from your current directory). For example, /Users/nikola/Documents is an absolute path, while Documents from your home directory is a relative path.

If you’re currently positioned in /var/www, then doing cd Documents will probably give you an error that the Documents folder doesn’t exist. However, if you use the full path (cd /Users/nikola/Documents), it will work.

9. Advanced File Operations

  • find
    • A powerful tool for searching files and directories. It’s like having a GPS for your files.
    • Examples:
    • find . -name "filename.txt" – find files by name
    • find /path/to/search -name "*.txt" – find files with a specific parttern
    • find /path/to/search -size +100M – find files based on size
    • find /path/to/search -perm 644 – find files with a certain permission
  • locate
    • Used for finding the location of files and directories
    • Example: locate filename.md

10. Networking Commands

  • ping
    • Check if you can reach another computer or server.
    • Example: ping example.com
  • curl
    • Fetch data from or send data to a server, especially useful for testing APIs.
    • Examples:
    • curl http://example.com – fetch a web page
    • curl -O http://example.com/somefile.zip – download a file
    • curl -d "param1=value1&param2=value2" -X POST http://example.com/form – send POST data
    • curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://example.com/api/login – pass headers
    • curl -b "name=value" http://example.com – use cookies

Additional things that could be useful to you

11. Exploring Environment Variables

  • Environment variables are key-value pairs used by your operating system to manage various system properties. They come in handy for storing data like file paths and configurations.
  • Example: use echo $VARIABLE_NAME to view the value of an environment variable, and export VARIABLE_NAME=value to set one.

12. File Compression and Archiving

  • tar, gzip, zip
    • These commands are used for compressing and decompressing files. They are crucial for managing file sizes and preparing data for transfer.

13. Remote File Transfer

  • scp (Secure Copy)
    • A command for securely transferring files between your local machine and a remote server. It’s like FedEx for your files.
    • Example: scp username@remote_host:/path/to/remote/file.txt /path/to/local/directory
  • rsync
    • A utility for efficiently transferring and synchronizing files across computer systems.
    • Example: rsync -av /path/to/local/dir username@remote_host:/path/to/remote/dir

14. Monitoring Disk Usage

  • df
    • Displays the amount of disk space used and available on your file systems.
  • du
    • Estimates the space used by files and directories.

15. Managing Processes

  • ps
    • Reports a snapshot of the current processes.
  • kill
    • Send a signal to a process, typically used to stop a process.

16. Networking and Port Management

  • netstat
    • Displays network connections, routing tables, and interface statistics.
  • ifconfig
    • Used to configure, or view the configuration of, a network interface.

17. Exploring System Logs

  • Logs are a goldmine of information. Use cat or less to view log files typically stored in /var/log and understand what’s happening under the hood.

18. Crontab for Scheduling Tasks

  • crontab is used for scheduling tasks to run at specific times. It’s like setting a smart alarm for your scripts.
    • Examples
    • with crontab -e command, you’ll be able to edit the crontab file
    • if you want to run a script /path/to/script.sh every day at 5:30 PM, you would add the following line: 30 17 * * * /path/to/script.sh

19. Command History

  • Your Terminal remembers your past commands. Use the history command to recall previous commands or Ctrl + R to search through your command history interactively.

20. Linking Directories with Symbolic Links:

  • ln -s
    • create symbolic links to files and directories. It’s like creating shortcuts.
    • Example: ln -s /path/to/original.txt /path/to/link.txt

21. Managing Users and Groups

  • useradd, usermod, groupadd
    • These commands are used for creating and modifying users and groups. They’re important for managing access on multi-user systems.

22. Disk Partitioning and Mounting

  • fdisk, mount
    • These commands are for managing disk partitions and mounting filesystems. They’re essential for organizing and accessing different storage devices.

23. Networking with nc and telnet

  • nc and telnet
    • These tools can be used for various network operations like port scanning, sending raw data to ports, etc.

24. Advanced Text Processing

  • cut, sort, uniq, tr
    • These text-processing commands are powerful tools for data analysis and manipulation in the command line. Check them out

25. System Information Commands

  • uname, lscpu, lshw
    • These commands provide detailed information about your system’s hardware and software configuration.

26. Learn/Customize Terminal Shortcuts

  • Learn keyboard shortcuts for your Terminal for efficiency.
  • Examples:
    • Ctrl + A to go to the beginning of the line
    • Ctrl + E for the end
    • Ctrl + K to delete from cursor to the end

27. Checking System Health and Resources

  • vmstat, iostat
    • Use these commands to monitor system performance and resource utilization.

28. Exploring System Services and Daemons

  • systemctl, service
    • These commands help you manage system services and daemons in Linux.

29. Filesystem Check and Repair

  • fsck
    • Check and repair Linux filesystems. It’s an essential tool for system maintenance.

30. Securely Deleting Files

  • shred
    • Securely delete files from your filesystem so that they are nearly impossible to recover.

13 additional things you could look into

  • Customizing Your Terminal
    • You can customize the look and feel of your Terminal. Play around with .bashrc or .zshrc (depending on your so-called shell) to change your prompt, add aliases for commands, or even add some fun colors.
    • Or, just install Oh My ZSH!
  • Scripting and Automation
    • Once you’re comfortable, start exploring shell scripting. You can automate repetitive tasks with simple scripts, making your life a whole lot easier.
  • Version Control with Git
    • The Terminal is a friend of Git, a popular version control system. Here’s one tutorial I wrote: Learn Git fast as if your job depends on it
  • Understanding Permissions
    • Files and directories have permissions that determine who can read, write, or execute them. Commands like chmod and chown help you manage these permissions.
  • System Information and Monitoring
    • top or htop
    • These commands give you a real-time overview of what your system is up to. It’s like a dashboard for your computer’s engine.
  • Working with Text
    • awk, sed
    • These are text-processing utilities. They’re complex but incredibly powerful for manipulating text files.
  • The Power of Wildcards
    • Learn how to use wildcards (*** and ?**) for matching file patterns. It’s like casting a net to catch specific files.
  • The Almighty Root User
    • Be cautious with the sudo command – it gives you root (administrator) privileges. With great power comes great responsibility.
  • Package Management
    • Get familiar with package managers like apt for Ubuntu or brew for macOS. They are like app stores for your Terminal.
  • Understanding Filesystem Hierarchy
    • Explore the standard filesystem hierarchy in Linux/Unix systems (/etc for configurations, /var for variable files, /home for user directories). It’s like learning the layout of a new city.
  • Understanding the Root Directory Structure
    • Each directory under / (root) in Unix-like systems has a specific purpose (/bin for binaries, /lib for libraries). Knowing this structure helps you navigate the filesystem more efficiently.
  • Exploring Regular Expressions
    • Regular expressions are patterns used to match character combinations in text. They are a powerful tool in command-line text processing.
  • Exploring Different Filesystems
    • Learn about different filesystems (ext4, NTFS, FAT32) and their characteristics.

Conclusion

There you have it – an extensive beginner’s guide to the Terminal, pushing well past the basics.

Remember, it’s all about practice and exploration. The command line is a powerful tool in your developer toolkit, so embrace it, play with it, and watch as your computer skills reach new heights.

Welcome to the club of command line aficionados, and as always, happy coding! 💪

Linux, Quick tips

Installing CasperJS on RHEL Linux distribution

First you have to install NodeJS. You can do that via package manager for your distribution. In my case (RHEL) it is easily done with:

sudo yum install nodejs npm --enablerepo=epel

Installing CasperJS is easy via npm (Node package manager):

sudo npm install -g casperjs

You will get output similar to this:

Copying extracted folder /usr/lib/node_modules/casperjs/node_modules/phantomjs/phantomjs/phantomjs-1.9.7-linux-x86_64.tar.bz2-extract-1396259124118/phantomjs-1.9.7-linux-x86_64 -> /usr/lib/node_modules/casperjs/node_modules/phantomjs/lib/phantom
Writing location.js file
Done. Phantomjs binary available at /usr/lib/node_modules/casperjs/node_modules/phantomjs/lib/phantom/bin/phantomjs
/usr/bin/casperjs -> /usr/lib/node_modules/casperjs/bin/casperjs
npm WARN unmet dependency /usr/lib/node_modules/block-stream requires inherits@'~2.0.0' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/fstream requires inherits@'~2.0.0' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/fstream-ignore requires inherits@'2' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/fstream-npm requires inherits@'2' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/glob requires inherits@'2' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/npmconf requires inherits@'~2.0.0' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
npm WARN unmet dependency /usr/lib/node_modules/tar requires inherits@'2' but will load
npm WARN unmet dependency undefined,
npm WARN unmet dependency which is version undefined
[email protected] /usr/lib/node_modules/casperjs
âââ [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
[[email protected] ~]$ casperjs
Fatal: [Errno 2] No such file or directory; did you install phantomjs?
[[email protected] ~]$ /usr/bin/casperjs
Fatal: [Errno 2] No such file or directory; did you install phantomjs?
[[email protected] ~]$ /usr/lib/node_modules/casperjs/node_modules/phantomjs/lib/phantom/bin/phantomjs
phantomjs>
[[email protected] ~]$ /usr/lib/node_modules/casperjs/node_modules/phantomjs/lib/phantom/bin/phantomjs --version
1.9.7

To have the binaries in my path every time I log in I added this alias to my .bashrc:

alias phantomset="export PATH=$PATH:/usr/lib/node_modules/casperjs/node_modules/phantomjs/lib/phantom/bin/"

and my .bash_profile looks like this:

# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
phantomset
Linux

How to use crontab in specific use cases

Here I will post non standard and ‘out of the ordinary’ crontab settings. A note to myself (ANTM): crontab file in Centos is located in /etc/crontab. Lets start:

Run a cron job every 5 minutes but starting from minute 3.

Surely you can go about this like so:

3,8,13,18,23,28,33,38,43,48,53,58 * * * * /bin/mycommand

but there’s much cleaner way of doing it:

3-59/5 * * * * /bin/mycommand

More info can be found on this Stack Overflow question and also another option is suggested here and goes like this:

*/5+3 * * * * /bin/mycommand

Similarly

Run a cron job every 5 minutes

*/5 * * * * /bin/mycommand

 

Recent posts

  • Discipline is also a talent
  • Play for the fun of it
  • The importance of failing
  • A fresh start
  • Perseverance

Categories

  • Android (3)
  • Books (114)
    • Programming (22)
  • CodeProject (35)
  • Daily Thoughts (77)
  • Go (3)
  • iOS (5)
  • JavaScript (127)
    • Angular (4)
    • Angular 2 (3)
    • Ionic (61)
    • Ionic2 (2)
    • Ionic3 (8)
    • MEAN (3)
    • NodeJS (27)
    • Phaser (1)
    • React (1)
    • Three.js (1)
    • Vue.js (2)
  • Leadership (1)
  • Meetups (8)
  • Miscellaneou$ (77)
    • Breaking News (8)
    • CodeSchool (2)
    • Hacker Games (3)
    • Pluralsight (7)
    • Projects (2)
    • Sublime Text (2)
  • PHP (6)
  • Quick tips (40)
  • Servers (8)
    • Heroku (1)
    • Linux (3)
  • Stack Overflow (81)
  • Unity3D (9)
  • Windows (8)
    • C# (2)
    • WPF (3)
  • Wordpress (2)

"There's no short-term solution for a long-term result." ~ Greg Plitt

"Everything around you that you call life was made up by people that were no smarter than you." ~ S. Jobs

"Hard work beats talent when talent doesn't work hard." ~ Tim Notke

© since 2016 - Nikola Brežnjak