Categories
Tools

Useful Linux Commands — Users and Processes

Linux is an operating system that many developers will use.

Therefore, it’s a good idea to learn some Linux commands.

In this article, we’ll look at some useful Linux commands we should know.

sudo

We can run a command as another user with the sudo command.

It lets us run commands as root.

For instance, we can run:

sudo nano /etc/hosts

to open the /etc/hosts file with nano as root.

We can also run sudo -i to start a shell as root.

And we can run as another user with the -u flag:

sudo -u bob ls /users/bob

We run ls as bob .

su

The su command lets us switch the shell to another user.

For instance, we run:

su bob

to run the shell as bob .

When we’re done, we type exit to return to our own shell.

clear

clear lets us clear the screen of the terminal.

ctrl+l is a shortcut to clear the screen.

clear -x clears the screen but lets us go back to see previous work by scrolling up.

who

The who command lets us display the users logged into the system.

Each shell opened will be listed.

We can see the terminal used and the time and day the session was started.

The -aH flags tell who to display more info like idle time and process ID of the terminal.

who am i liust the current terminal session’s details.

whoami

The whoami command print the current user name.

which

which shows where the command is stored.

For instance, we run which ls to see where ls is stored.

type

The type command lets us determine the type of command we’re running.

A command can be one of 4 types:

  • an executable
  • a shell built-in program
  • a shell function
  • an alias

fg

The fg common put a job that’s running in the background into the foreground.

We can get the job ID of jobs with the job command.

And we can use the job ID as the argument of fg .

So we run:

fg 2

to put job with ID 2 into the foreground.

bg

bg resumes a job that’s been suspended.

We can get the job ID of jobs with the job command.

And we can use the job ID as the argument of fg .

So we run:

fg 2

to resume the job with ID 2.

jobs

jobs lets us list the status of the jobs we started.

alias

The alias command lets us create a shortcut to another command.

For instance, we can run:

alias ll='ls -al'

to create the alias ll to run the ls -al command.

killall

The killall command lets us send signals to multiple processes currently running.

Then general syntax is:

killall <name>

where name is the name of the program.

Conclusion

We can run commands to manage jobs and process with various Linux commands.

Categories
Tools

Useful Linux Commands — Password and Permissions

Linux is an operating system that many developers will use.

Therefore, it’s a good idea to learn some Linux commands.

In this article, we’ll look at some useful Linux commands we should know.

wc

The wc command lets us count lines, words, or bytes.

For instance, we run:

wc -l test.txt

to count the number of lines in test.txt .

We count the number of words in the file with:

wc -w test.txt

And we tet the number of bytes in the file with:

wc -c test.txt

The -m flag gets the correct byte value.

open

The open command lets us open files, directories, and programs.

We run

open <filename>

to open a file.

It also works with directory.

We can open the current directory with:

open .

passwd

The passwd command lets us change a user’s password.

We run it to get a prompt to enter the current password and the new password we want to set.

We can also run:

passwd <username> <new password>

to set a user’s password when we’re logged in as a superuser.

chmod

chmod lets us change file permissions.

To change it, we run chmod with the following letters:

  • a stands for all
  • u stands for user
  • g stands for group
  • o stands for others

Then we type in + to add a permission or - to remove it.

Then we enter one or more permission symbols:

  • r — read
  • w — write
  • x — execute

For instance, we run:

chmod a+r filename

to add the read permission to filename for all users.

We can add permissions for multiple roles by adding multiple letters before the + or - :

chmod og-r filename

Now we remove the read permission to filename for other and group.

We can apply permissions recursively with the -r flag.

We can also set file permissions with numbers.

It can be one of the following:

  • 0 no permissions
  • 1 can execute
  • 2 can write
  • 3 can write, execute
  • 4 can read
  • 5 can read, execute
  • 6 can read, write
  • 7 can read, write and execute

Then we run:

chmod 777 filename

We set the permission for user, group, and others with the digits.

7 is created by adding 1, 2, and 4 together.

This gives everyone full permission to work with filename .

chown

We can change the owner of a file or directory with the chown command.

The general format is:

chown <owner> <file>

Then we can change the permission by running:

chown user test.txt

We change the owner of test.txt to user .

Also, we can change the permission of all files and directories recursively with the -R flag:

chown -R <owner> <file>

Or we can change the owner to a group with:

chown <owner>:<group> <file>

So we can run:

chown bob:users test.txt

to change the owner to bob in the users group for test.txt .

We can also change the group of a file with the chgrp command:

chgrp <group> <filename>

Conclusion

We can change file permissions, count file sizes, and change passwords with some Linux commands.

Categories
Tools

Useful Linux Commands — Files and Histories

Linux is an operating system that many developers will use.

Therefore, it’s a good idea to learn some Linux commands.

In this article, we’ll look at some useful Linux commands we should know.

uname

The uname command lets us print details about the current machine and OS running on it.

We can use the -m switch to show the hardware name.

The -p switch prints the processor architecture.

The -s switch prints the OS name.

-r prints the release and -v prints the version.

The -n prints the node network name.

-a print prints everything.

man

man lets us open the help page for a given command.

We run man <command> to get help with a command.

Man pages are divided into 7 different groups, identified by a number:

  • 1 is user commands
  • 2 is kernel system calls
  • 3 is C library functions
  • 4 is devices
  • 5 is files formats and filesystems
  • 6 is games
  • 7 is miscellaneous commands, conventions and overviews
  • 8 is superuser and system administrator commands

grep

The grep command lets us search for text with a pattern/

For instance, we run:

grep -n document index.md

to search for the document keyword in the index.md file.

The -n switch lets us show the line numbers of the result.

We can also use grep to filter the output of another command.

To do this, we run:

less index.md | grep -n document

We open the index.md file with less , then we pipe the outline to the grep command to search for the word document in index.md .

Also, we can invert the result with the -v option to exclude a particular string.

umask

The umask command lets us set default permissions for files.

Running umask without arguments will show us the current mask.

The mask is the octal number representing the current permissions.

umask -S shows us the permissions with human-readable notation.

The digits return means the following:

  • 0 — read, write, execute
  • 1 — read and write
  • 2 — read and execute
  • 3 — read-only
  • 4 — write and execute
  • 5 — write-only
  • 6 — execute only
  • 7 — no permissions

We can set a new value by passing it in as an argument:

umask 002

We can also specify the permission by role:

umask g+r

du

We can run the du command to calculate the space usage of files and directories.

We run it to tet the list of items and their sizes in bytes.

The -a switch lets us print the size of each file in the directories.

We can sort the results with the sort :

du -h <directory> | sort -nr

history

The history command lets us view the command line history.

We can also use !<commnand number> to repeat the command with the given number from the history output.

To clear the command history, we run history -c .

Conclusion

We can list files, search outputs, and command history with various Linux commands.

Categories
Tools

Useful Linux Commands — Text and Arguments

Linux is an operating system that many developers will use.

Therefore, it’s a good idea to learn some Linux commands.

In this article, we’ll look at some useful Linux commands we should know.

xargs

The xargs command lets us pass output of a command and use it as an argument to another command.

The general syntax is:

command1 | xargs command2

For instance, we can write:

cat deleteall.txt | xargs rm

to the file paths in deleteall.txt to the rm command to delete all the files listed in deleteall.txt .

With the -p switch, we see a confirmation prompt before the command is run:

cat deleteall.txt | xargs -p rm

We can run multiple commands with the -I switch:

command1 | xargs -I % /bin/bash -c 'command2 %; command3 %'

The commands are the ones after the % .

df

The df command lets us get disk usage information.

We can use the -h switch to get the values in a human-readable format.

nohup

The nohup command lets us run a command that doesn’t end when the terminal is killed.

We just put the command after nohup to run it.

diff

diff lets us compare the content of 2 text files.

For example, we run:

diff dogs.txt dogs2.txt

to compare the content of dogs.txt and dogs2.txt .

The -y switch will make diff compare the 2 files line by line.

We can also use it to compare directories with the -r switch:

diff -r dir1 dir2

We can compare which files differ with the r and q switches together:

diff -rq dir1 dir2

uniq

The uniq command lets us get unique lines of text from a file.

For instance, we run:”

uniq dogs.txt

to get the unique lines of text from the dogs.txt file.

We can also use it to get unique lines of output from a command with the | operator:

ls | uniq

We can sort the lines before getting the unique lines with the sort command:

sort dogs.txt | uniq

We can display only the duplicate lines with the -d switch:

sort dogs.txt | uniq -d

The -u switch makes it only display the unique lines:

sort dogs.txt | uniq -u

The -c switch lets us count the occurrences of each line:

sort dogs.txt | uniq -c

sort

The sort command lets us sort lines of text or text command outputs.

For example, we run:

sort dogs.txt

to sort the lines in dogs.txt .

We can use the -r switch to sort the items in reverse order:

sort -r dogs.txt

And we can use the -u switch to return only the unique lines:

sort -u dogs.txt

We can also use sort to sort command outputs.

For instance, we can sort the output from the ls command with:

ls | sort

Conclusion

We can pass arguments to commands, and diff and manipulate text outputs with various Linux commands.

Categories
Tools

Useful Linux Commands — Environment Variables and Cron Jobs

Linux is an operating system that many developers will use.

Therefore, it’s a good idea to learn some Linux commands.

In this article, we’ll look at some useful Linux commands we should know.

env

The env command lets us set environment variables for the current session.

For instance, we can run:

env USER=username node app.js

to set the USER environment variable in the current session.

We can clear environment variables with the -i switch:

env -i node app.js

We can use the -u switch to make a variable inaccessible:

env -u USER node app.js

Now the USER environment variable won’t be visible to programs in the session.

printenv

The printenv command lets us print environment variables onto the screen.

We can print a specific one by specifying it after the command.

For instance, we run:

printenv USER

to print the value of the USER environment variable.

basename

The basename command lets us return the file name portion of the path.

For instance, we run:

basename /users/foo/test.txt

to print test.txt onto the screen.

If the last segment is a directory, then we get that directory’s name.

dirname

The c command lets us get the direction portion of a path.

For instance, if we have:

dirname /users/foo/test.txt

Then we get /users/foo displayed on the screen.

crontab

The crontab command lets us schedule cron jobs.

We can list the corn jobs scheduled with the -l switch:

crontab -l

We can run:

crontab -e

to add new cron jobs.

The syntax for setting the schedule for a cron job is the following.

To set the minute that the cron job runs, we can add one or more of the following:

  • * — any value
  • , — value list separator
  • - — range of values
  • / — step values

To set the hour that the cron job runs, we can add one or more of the following:

  • * — any value
  • , — value list separator
  • - — range of values
  • / — step values
  • 0–23

To set the day of the month that the cron job runs, we can add one or more of the following:

  • * — any value
  • , — value list separator
  • - — range of values
  • / — step values
  • 1–31

To set the month that the cron job runs, we can add one of the following:

  • * — any value
  • , — value list separator
  • - — range of values
  • / — step values
  • 1–12

To set the day of the week that the cron job runs, we can add one of the following:

  • * — any value
  • , — value list separator
  • - — range of values
  • / — step values
  • 0–6 — (0 is Sunday to 6 is Saturday)
  • SUN-SAT

For instance, we can run:

30 08 10 06 * /home/ramesh/full-backup

to set the full-backup script to run at a specified time.

The expression means:

  • 30–30th Minute
  • 08–08 AM
  • 10–10th Day
  • 06–6th Month (June)
  • ***** — Every day of the week

Conclusion

We can use Linux commands to get and set environment variables and view cron jobs.