9 Terminal Commands You Can Start Using Today

Posted on

Terminal commands to increase your efficiency

Hand holding a card that reads, “what can you do today that you couldn’t do a year ago?”

Hand holding a card that reads, “what can you do today that you couldn’t do a year ago?”

Photo by Mikel Parera on Unsplash

If you use the terminal every day, you must know quite a few commands. In this article, we cover the cut command to remove characters, the paste command to create columns, invert matching using grep, the JSON processor, easy renaming, how to copy, delete, and paste lines using the sed command, and more.

So fire up your terminal and let’s get started!

The cut command removes some characters from each line of a file. The following will output the fifth to tenth character position from each line in the file sample.txt:

$ cat sample.txt

001 one

002 two

003 three

004 four

005 five
❯ cat sample.txt | cut -c 5-10

one

two

three

four

five

The -c option selects only characters and 5-10 tells them to be between the fifth and tenth characters.

Image for post

Image for post

cut command in action. Image by Author.

The paste command merges lines of files. You can also use it to create columns.

$ seq 1 10 | paste - - -

1 2 3

4 5 6

7 8 9

10

paste — — will create two columns and paste — — — — will create four columns.

Image for post

Image for post

The paste command in action. Image by Author.

You can use the -d option to add delimiters. The following code will add a comma between states and create four columns, then save it to another file.

$ cat statenames

Alabama

Alaska

Arizona

Arkansas

California

Colorado

Connecticut

Delaware

Florida

Indiana

Iowa

Kansas

...

$ cat statenames | paste -d, - - - - > myfile

$ cat myfile

Alabama ,Alaska,Arizona,Arkansas

California,Colorado,Connecticut,Delaware

Florida,Indiana,Iowa,Kansas

The ls /bin /usr/bin lists contents from the /bin and /urs/bin directories:

Image for post

Image for post

The output of ls /bin /usr/bin. Image by Author.

The output contains /bin: and /usr/bin:.

We can use the grep -v pattern to output all lines that don’t match the pattern. We can use a pipe | to send the output of ls to the standard input of grep command.

The grep -v '/bin:$' command outputs all lines except /bin: and /usr/bin:. The $ anchor matches the end of a line.

$ ls /bin /usr/bin | grep -v '/bin:$' | paste - - - - | less

Image for post

Image for post

The output of ls /bin /usr/bin | grep -v ‘/bin:$’ | paste — — — — | less. Image by Author

I used paste — — — — to create four columns.

The ^$ expression matches empty lines:

$ cat sample.txt

001 one

002 two
003 three004 four

005 five
$ cat sample.txt | grep -v '^$'

001 one

002 two

003 three

004 four

005 five

The file sample.txt contains empty lines. grep -v '^$' outputs the file content without empty lines.

We can use this with the previous command:

$ ls /bin /usr/bin | grep -v '/bin:$' | grep -v '^$' | sort | paste - - - - > mylist

$ cat mylist

2to3- 2to3-2.7 AssetCacheLocatorUtil AssetCacheManagerUtil

AssetCacheTetheratorUtil BuildStrings CpMac DeRez

GetFileInfo IOAccelMemory IOMFB_FDR_Loader MergePef

MvMac ResMerger Rez RezDet

...

I added grep -v '^$', which outputs all lines that don’t match an empty line, then sorts them, creates four columns, and writes it to the mylist file.

You can find all files recursively using the find command. The find . -name "foo*" searches for all the files in the current directory where the file’s name start with foo:

$  tree

.

├── dir1

│ ├── foo4.txt

│ ├── foo5.txt

├── foo1.txt

├── foo2.txt

└── foo3.txt

$ find . -name "foo*"

./foo1.txt

./dir1/foo4.txt

./dir1/foo5.txt

./foo3.txt

./foo2.txt

jq is a command-line JSON processor. When you use the curl command for an API, you get returned values in JSON format. You can use the jq utility to process the data.

The .[] syntax, omitting the index entirely, will return all of the elements of an array.

$ curl https://jsonplaceholder.typicode.com/users | jq '.[]'

Image for post

Image for post

The jq .[] returns all elements. Image by Author.
$ curl https://jsonplaceholder.typicode.com/users | jq '.[0]'

jq ‘.[0]’ will return the first element.

Image for post

Image for post

The jq command example. Image by Author.

You can get the company name of the first element:

curl https://jsonplaceholder.typicode.com/users | jq '.[0] | {name: .company.name}'

Image for post

Image for post

The jq command example 2. Image by Author.

The | operator in jq feeds the output of .[0] into the input of {...}. This builds an object out of the field. You can access nested attributes using .company.name.

You can get multiple fields:

$ curl https://jsonplaceholder.typicode.com/users | jq '.[0] | {name: .company.name, lat: .address.geo.lat}'

Image for post

Image for post

The jq command example 3. Image by Author.

Find out more about jq.

The bc supports arbitrary-precision numbers.

In computer science, arbitrary-precision arithmetic, also called bignum arithmetic, multiple-precision arithmetic, or sometimes infinite-precision arithmetic, indicates that calculations are performed on numbers whose digits of precision are limited only by the available memory of the host system.

Arbitrary-precision arithmetic

This means it supports not only basic calculations but also big number arithmetic as well.

❯ bc --version

bc 1.06

Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.

❯ bc

bc 1.06

Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.

This is free software with ABSOLUTELY NO WARRANTY.

For details type `warranty'.

5 + 12

17

5+11

16

1000/2

500

10/3

3

quit

The bc command can take input from a pipe:

$ echo '10*2/5' | bc

4

Let’s find out if we can calculate 100!:

$ echo {1..100} | tr ' ' '*' | bc

93326215443944152681699238856266700490715968264381621468592963895217

59999322991560894146397615651828625369792082722375825118521091686400

0000000000000000000000

The echo {1..100} command outputs 1 2 3 ... 100, tr translates a space to an * and bc will do the bignum arithmetic.

The sed command is a stream editor that performs basic text transformations. It allows you to replace, remove, and insert in a text file.

One of the more well-known functions of sed is to copy, delete and paste a line. You can use sed 'h;d;G' to copy and delete the nth line and paste it to the mth line:

$ cat sample.txt

one

two

three

four

five

six

seven

$ cat sample.txt | sed '4h;4d;6G'

one

two

three

five

six

four

seven

This copies and deletes the 4th line and pastes it to the 6th line.

You can copy a line and paste it (not deleting):

$ cat sample.txt | sed '4h;6G'   

one

two

three

four

five

six

four

seven

Or just delete lines:

$ cat sample.txt | sed '4d;6d'

one

two

three

five

seven

Or do more complicated things:

$ cat sample.txt | sed '4h;4d;6G;5h;7G'

one

two

three

five

six

five

seven

five

The rename command renames multiple files.

$ ls

foo.htm bar.htm baz.htm

$ rename .htm .html *.htm

$ ls

foo.html bar.html baz.html

The code above changes the file extension .htm to .html. You can modify file names by prepending and appending strings.

Prepend my- to all files:

$ ls

file-1 file-2 file-3 file-4
$ rename -A "my-" *

$ ls

my-file-1 my-file-2 my-file-3 my-file-4

Append .txt to all files:

$ rename -a ".txt" *

$ ls

my-file-1.txt my-file-2.txt my-file-3.txt my-file-4.txt

You can also substitute using the -s or -S option. The -s option substitutes the first occurrence and the -S option substitutes all.

Substitute with _ in all occurrences using the -S option:

$ rename -S "-" "_" *

my_file_1.txt my_file_2.txt my_file_3.txt my_file_4.txt

I hope these commands are useful and you can start using them.

If you forget how to use them, the man command gives you all the options. Or you can use alternative tools here.