Categories
Tools

Useful Linux Commands — File Operations and Multiple Commands

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.

less

The less command lets us show the content of a file.

The general syntax is:

less <filename>

We can navigate with arrow keys.

Spacebar and b navigates page by page.

/ lets us search for content.

? search backward.

F enters follow mode. When the file is changed, the changes are displayed live.

ctrl+c quits follow mode.

cp

cp lets us move files and folders.

For instance, we run:

cp foo bar

to copy foo to bar .

We can also use it to copy folders with the -r switch:

cp -r fruits cars

We move the fruits folder contents to cars .

mv

The mv command lets us move files and folders.

For instance, we run:

mv foo bar

to move foo to bar .

We can move files into a folder with:

mv grape banana fruits

We move the grape and banana files to the fruits folder.

ls

The ls command lets us list files and folders.

We can list the content of a given folder with:

ls /bin

We can add the a switch to show hidden files.

l shows files permissions, file and folder sizes, and modified date time.

We run ls -al to show all that info.

rmdir

The rmdir command lets us remove a folder.

We run:

rmdir fruits

to remove the fruits folder.

We can use it to remove multiple folders:

rmdir fruits cars

We remove the fruits and cars folders.

The folders we delete must be empty.

To delete non-empty folders, we run:

rm -rf fruits cars

-r means recursive and f means force.

pwd

pwd shows the current working directory.

cd

cd lets us change the current working directory.

For instance, we run:

cd fruits

to go to the fruits folder.

cd .. to move to the home folder.

cd ../cars moves to the parent folder of cars .

We can use absolute paths with cd , so we run cd /etc to go to the /etc folder.

mkdir

mkdir lets us create a folder.

For instance, we run:

mkdir cars

to create a cars folder in the current working directory.

And we run:

mkdir dogs cars

to create the dogs and cars folders.

We can create multiple nested folders with the -p switch:

mkdir -p fruits/apples

!!

!! lets us run the last command.

; / && / &

; lets us run one command after the other like:

ls; pwd

&& runs multiple commands but the ones on the right won’t run if the left one fails.

& lets us run multiple commands in parallel instead of waiting for the current one to finish before running the next one.

Conclusion

We can run commands to show file content and run multiple commands with some operators with Linux.

Categories
Tools

Useful Linux Commands — Soft Links and Files

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.

Soft Links

We can create soft links with the ln -s command.

The general syntax is:

ln -s <original> <link>

Soft links will be broken with the original file removed.

For instance, we run:

ln -s foo.txt newfoo.txt

to create the newfoo.txt soft link.

We can see that the soft link should have th @ suffix and it’s colored differently when we run the ls -al command.

find

The find command lets us find files and folders on the file system.

For instance, we can find all files with the .txt extension by running:

find . -name '*.txt'

We need quotes around special characters like * to stop the shell from interpreting them.

We can also he it to find directories with the -type d switch:

find . -type d -name src

-type f lets us search for files only.

-type l lets us search only symbolic libks.

We can also search under multiple root trees:

find folder1 folder2 -name foo.txt

We can also search with multiple keywords with -or :

find . -type d -name node_modules -or -name public

We can exclude results with -not :

find . -type d -name '*.md' -not -path 'node_modules/*'

And we can search for files with at least with the given size with -size :

find . -type f -size +100c

c means bytes.

We can search for files with size in a range with:

find . -type f -size +100k -size -1M

We can search for files that are edited more than a given number of days ago with -mtime :

find . -type f -mtime +3

And we can delete files that are found with the -delete option:

find . -type f -mtime -1 -delete

cat

cat lets us add content to a file.

To print content to standard output, we run:

cat file

To print the content of multiple files, we run:

cat file1 file2

We can redirect the output to a file by running:

cat file1 file2 > file3

We can change > to >> top create the file if it doesn’t exist.

And we can print line numbers with -n :

cat -n file

touch

The touch command lets us create an empty file.

For instance, we run:

touch file

to create a file named file .

If it already exists it opens the file in write mode and the timestamp is updated.

tail

The tail command lets us show the content at the end of a file,

The -f switch watches for file changes and updates the output automatically.

For instance, we run:

tail -f /foo.txt

to watch foo.txt and update the output.

We can change the number of lines printed with -n :

tail -n 10 <filename>

We print the last 10 lines with -n 10 .

We can print the whole file content starting from a specific line with + before the line number:

tail -n +10 <filename>

Conclusion

We can create soft links and create and manipulate files with Linux commands.

Categories
Tools

Useful Linux Commands — Processes and Links

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.

kill

The kill command lets us send signals to a process.

The general format is:

kill <PID>

where PID is the process ID.

We can send signals like:

kill -HUP <PID>
kill -INT <PID>
kill -KILL <PID>
kill -TERM <PID>
kill -CONT <PID>
kill -STOP <PID>

HUP means hang up. It’s sent when a terminal window that started a process is closed before terminating it.

INT means interrupt. It sends the signal when we press ctrl+c.

KILL is sent to the OS kernel to stop and terminate the process.

TERM means terminate. The process that receives it will terminate.

CONT means continue. It lets us resume a stopped process.

STOP is sent to the OS kernel instead of the process, which stops but doesn’t terminate the process.

We can also use a number to replace these signals. 1 is HUP , 2 is INT , 9 is KILL , 15 is TERM , 18 is CONT .

top

The top command lets us list processes running in real time.

We can quit top with ctrl+c.

And we can sort processes by the amount of memory used by running:

top -o mem

echo

echo lets us print arguments passed to it.

For instance, we run:

echo "hello" >> output.txt

to put hello into the output.txt file.

Also, we can interpolate environment variables in the string:

echo "path=$PATH"

We have to escape special characters like $ in the command.

We can echo the files in the current folder with:”

echo *

And we can echo the files that start with a with:

echo a*

We can print the home folder path with:

echo ~

We can also print the results of a command with $() like:

echo $(ls)

ps

The ps command lets us list the processes currently running in the system.

We can list all processes with the ps ax command.

a is used to list other users’ processes.

x shows processes not linked to any terminal.

ps axww continues the command listing on a new line instead of truncating it.

The output has the PID which is the process ID, TT tells us the terminal ID used.

STAT tells us the state of the process.

I means the process is idle. R is a runnable process. S is a process that’s sleeping for less than 20 seconds.

T is a stopped process, U is a process in uninterruptable wait and Z is a dead process.

+ means the process is in the foreground. s means the process is a session leader.

ln

ln lets us create links in the filesystem.

The syntax of the command is:

ln <original> <link>

For instance, we run

ln foo.txt newfoo.txt

to create the newfoo.txt link to the foo.txt file.

Links are indistinguishable from a regular file from the user’s perspective.

Conclusion

We can manage processes and create symbolic links with Linux commands.

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.