DEV Community

loading...

Useful Linux Commands — File Operations and Multiple Commands

aumayeung profile image John Au-Yeung Originally published at thewebdev.info ・2 min read

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

to copy foo to bar .

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

cp -r fruits cars
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

to move foo to bar .

We can move files into a folder with:

mv grape banana fruits
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

to remove the fruits folder.

We can use it to remove multiple folders:

rmdir fruits cars
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

-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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

to create a cars folder in the current working directory.

And we run:

mkdir dogs cars
Enter fullscreen mode Exit fullscreen mode

to create the dogs and cars folders.

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

mkdir -p fruits/apples
Enter fullscreen mode Exit fullscreen mode

!!

!! lets us run the last command.

; / && / &

; lets us run one command after the other like:

ls; pwd
Enter fullscreen mode Exit fullscreen mode

&& 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.

Discussion (0)

pic
Editor guide