Categories
Tools

Essential Linux Command Line Programs

If we work as developers, we have to work with the Linux command line eventually.

In this article, we’ll look at some basic Linux programs we should know to work with the Linux command line.

curl

curl lets us make HTTP requests to a server a get a request back.

We can use it to specify the headers and body of the request in addition to any cookies.

python -m json.tool / jq

python -m json.tool and jq are 2 separate programs that we can use to pretty-print JSON so that we can read them easily.

They all work after the pipe. For instance, we can run both as follows assuming test.json has JSON text in it:

cat test.json | jq

or:

cat test.json | python -m json.tool

ls

ls lists the files and directory in a directory.

Once we run this, we get the permissions of the files and folders and when they changed.

tail

tail shows us the end of a text file. As the text file updates with new content, it’ll show the latest and keep showing the end of the file if new things are added if we add the -f option

cat

cat is used to concatenate and print files on the screen. We just add the paths that we want to the end of the list and the content of all the files will be concatenated one after the other on the screen.

grep

grep lets us search for patterns from the output of another command.

This is very handy for searching for data from any large blocks of text.

ps

ps shows the status of the running processed in our computer.

The date, process ID, and time are all included.

env

env prints out environment variables. It’s useful if environment variables are added incorrectly.

top

top shows us the number of resources used by the currently running processes in our system.

netstat

netstat shows us the network status of our system. It shows us the ports that are used and the IP address they connected from.

Also, we can see the processes that are using network connections in one table.

ip address

The iproute2 package has the ip address programs that show us which interfaces are connected to which network.

lsof

The lsof package shows us the item that are listening for incoming data in our network.

it lists the ports that it’s listening to and the ID of the process that’s listening to a given port.

df

The df program shows us the free disk space that’s on our system.

We can see which volumes are full and which aren’t.

du

We can get more information our disk volumes than df with du .

The -h flag makes the output human-readable and -s shows the total size.

id

The id command returns the currently logged in user’s identify.

chmod

chmod is used to change the permissions of our files and directories.

This can help us fix permission errors when we encounter them.

dig / nslookup

dig and nslookup let us troubleshoot DNS errors. We can see whether we can resolve the some URL to their IP address with it.

Just put the URL we want to find the IP address after each and check the output.

iptables

iptables is used to block or allow traffic on a Linux system. It’s similar to a network firewall.

We can whitelist and blacklist by IP address with this package.

sestatus

We can check if we have SELinux enabled with this command. Then we can check if it’s enabled or not.

history

history shows us the commands that we’ve run recently. It’s easy to look at what we’ve run so we can run them again.

tar

tar can be used to created archives and uncompress them.

We create one by running tar cvf archive.tar /dirname to create an archive out of the /dirname directory.

The extract data from an archive, we run tar xvf archive.tar .

To view an archive, we run tar tvf archive.tar .

Conclusion

There’re many commands that we can use to do things on the Linux command.

Commands like curl and tar are important for making requests and extracting files.

There’re also packages that we can install like jq to pretty-print JSON.

And there’re many more that’ll come in handy for troubleshooting networks and processes.

Categories
Tools

Basic Linux Commands We Should All Know

If we work as developers, we’ve to work with the Linux command line eventually.

In this article, we’ll look at some basic Linux commands we should know to work with the Linux command line.

pwd

pwd stands for Print Working Directory. It shows the directory that we’re in now.

ls

ls lists the files and directories that are in the current directory.

cd

cd stands for change directory. It lets us change directory to a different directory by typing in the directory name we want to switch to after cd.

mkdir

mkdir lets us make a new directory in the current working directory by typing in the directory name after mkdir.

rmdir

rmdir is used to remove the directory with the given name.

lsblk

Lists the available block devices in a system. A block device gives us access to a device in a way where the hardware characteristics of the device isn’t visible.

mount

We can use the mount command to mount a removable storage device. This makes it available for us to use.

df

The df lets admins monitor and analyze server status and network systems.

uname

Gets the Linux version information of the operating system we’re currently using.

ps

Lists the tasks that currently running on our system.

kill

kill lets us end tasks that are currently hung in our system.

service

The service command lets us run system-wide services from the terminal.

batch

batch lets us run system services in a predefined schedule.

shutdown

shutdown does what its name says. It shuts down our computer.

touch

touch lets us create a new text file by typing in a file name after it.

cat

cat is used to concatenate multiple files by specifying file paths of text files after it to concatenate them together.

head

The head command lets us view the head of the file by typing in the file path of the text file we want to view after it.

tail

The tail command lets us view the tail of the text file as it’s being updated. The updates are done live so we’ll always see the latest.

cp

cp lets us copy files from one place or another and if it’s the same directory, it’ll rename the file.

mv

mv is short for a move. It lets us move files around our system. We can use the -f switch to force the operating system to move big files.

comm

The comm command is the command to compare 2 files.

less

The less command is popular for viewing the contents of files. We can let users navigate within a file in both directions with this file.

ln

ln is used for creating symbolic links to some files for easy access.

cmp

We can use cmp to compare files and output the results to an output stream.

dd

The dd command is used to convert and copy files. We can use it to make hardware and device files appear in the file system like normal files.

After running dd, those devices will show up like regular files.

alias

The alias command lets us replace a word by another string. This helps us replace long commands with shorter ones.

cal

This is a small calendar program that we can run to get some information about today’s date and other things.

fortune

The fortune command lets us see the fortune that we get for the day. It has funny and inspirational quotes.

history

The history commands prints out the commands that we typed and see what we’ve done in the terminal recently.

yes

Typing yes and then some text after it just displays the text after yes forever until we stop it with Ctrl+C.

banner

The banner command gives us the ability to make our own ASCII banners that we can see on the command line.

rev

The rev command takes text input and writes them to the standard output and reversing each character. It’s useful for pranks.

wget

wget lets us download files from the Internet right from the terminal. It works with HTTP, HTTPS, and FTP servers.

iptables

iptables lets sysadmins control incoming and outgoing traffic on a host machine. It’s used on a regular basis to define authentic traffic and blacklist suspicious or untrusted requests.

Conclusion

Linux has lots of commands that we should know about.

We should know how to traverse directories, list files, and copy/moves files.

Also, knowing how to download files is also handy.

Categories
Tools

List of Useful Git Commands

Git is one of the most popular version control systems in the world. It saves us lots of frustration by letting us interact with the change history and revert code.

In this article, we’ll look at more ways that Git can help us with some commands that we may have missed.

Find Branches

We can find a branch with a given commit by running git branch --contains <commit>

Modify The Most Recent Commit

We can use git commit --amend to amend the last commit.

With this, we can change the commit message or the files that were committed in the last commit.

Or we can add the --no-edit option to amend the last commit without changing the last commit message.

Selected Files to Commit Interactively

We can use the git add -p command to select files interactively to add to out commit.

-p is short for --patch .

Choose Files to Stash Interactively

We can use the git statsh -p command to pick files that we want to stash.

Again -p is short for --patch .

Stash Untracked Files

The git stash -u command lets us stash untracked files. There’s also the -a or --all option which does the same thing.

Revert Selected Files

To let us selectively revert files, we can use the git checkout -p command, which again -p stands for --patch , to let us selective revert files that have been changed.

Switch to a Previous Branch

We can switch to a previous branch with the git checkout - command.

- is an alias for the previous branch.

Revert All Local Changes

The git checkout . command lets us revert all local changes.

Show Changes

We can use the git diff --staged command to let us compare the staged changes to what’s currently committed.

Rename Branches Locally

To rename branches locally, we can run:

git branch -m old-branch-name new-branch-name

to change the name of the current branch from old-branch-name to new-branch-name .

Rename Branches Remotely

We can rename the rename a remote branch by running:

git push origin :old-branch-name  
git push origin new-branch-name

The first command removes the old branch with old-branch-name and the 2nd command pushes the new branch with new-branch-name to our remote Git server.

Open All Files with Conflicts at Once

We can open all files that have conflicts by running:

git diff --name-only --diff-filter=U | uniq  | xargs $EDITOR

— diff-filter=U filters out all the files without conflicts.

Find Files That Have Changed From a Given Date and On

Git has a whatchanged command to let us find files from a given period. We can run it by typing:

git whatchanged —-since=‘3 weeks ago’

Then Git will show us all the files that are changed since 3 weeks ago.

Remove a File From the Last Commit

We can use the rm command to remove the old file and then run git commit --amend to amend the last commit to remove the file.

So we run:

git rm —-cached file-to-remove  
git commit —-amend

one after the other to remove the file file-to-remove then run git commit —-amend to amend the last commit to remove the file from the commit.

Overwrite Local With Remote

We can overwrite our local files with files in the remote repository with git reset --hard origin/<branch_name>.

This will reset the history to whatever is in the remote repository.

Remove All Untracked Files

We can run git clean -d -n to do a dry run to see the files that’ll be removed and git clean -d -f to do the removal if we’re fine with the changes.

Stashing Changes

We’ve stash changes if we pull from a remote repository and it conflicts with what we have in the local one.

Stashing is storing modified tracked files temporarily.

To stash changes, we run git stash save . Then once we’re done pulling from remote, we can run git stash pop to restore the changes that were stashed.

Conclusion

There’re lots of commands that we can use that we may not have used. So these are going to help with improving our Git workflow.