DEV Community

Seb
Seb

Posted on

Working with Files in Linux πŸ’»

Hey Dev community! πŸ˜†

After weeks of exams and dealing with imposter syndrome, I am back with another short article on 'file commands' in Linux. These are pretty useful so take a look. 🀠

1. cd πŸ’»

The 'cd' command moves you into any directory you want. For example, if I have files located in the 'Documents' directory, this is the command I would run:

cd Documents

and this would move me to that directory.

cd

2. ls πŸ’»

The 'ls' command lists everything that is in that directory. To list everything in the directory, run this command:

ls -l

ls

3. cat πŸ’»

the 'cat' commands displays what is inside a file. Simply run this command:

cat (name of file)

cat

The cat command also can do other things. πŸ‘‡

🎯 cat > filename

Using this command, you can also create a file.

What I did below was I created the file, typed out what I needed to, then viewed the file which displayed the text I had previously written.

cat

🎯 cat testfile1 testfile2>testfile4

Using this command, you can also create 1 new file by putting two together by doing this command:

What I did was I used test1, and test2, and merged that into a new file called test4.

cat

4. cp πŸ’»

The 'cp' commands copies files from one directory to another

What I did below was I copied the Goku image from the pictures directory to the example directory, used 'cd' to move into the example directory, and did ls -l to view if the picture successfully copied.

cp

5. mv πŸ’»

The 'mv' command is typically for moving files but it can also be used to rename files as well.

🎯 mv (to move)

To move a file into a directory, run this command:

mv (filename) (directory)

What I did below was I moved test1 from Documents to example1, I went to example1, and then listed the files to confirm if it successfully moved.

mv

🎯 mv (to rename)

To rename a file, run this command:

mv (filename) (newfilename)

Staying in the example1 directory, I changed the name to 'newname' and then listed the files to confirm it's been changed.

mv

6. mdkir πŸ’»

The 'mkdir' command is what you will use almost daily. This command is used to make directories.

πŸ’»

7. grep πŸ’»

'grep' is used to search for certain words in a text. To do this, run the command

grep (word) (file name)

grep

8. touch πŸ’»

'touch' is another command you can use to create a file.

touch

9. rm

'rm' is used to delete directories or anything within them. Always be careful with this command because there is no going back once you remove something. To delete, run this command:

rm -r (name of directory or file)

rm

10. head

'head' is used to view the first lines of a text file. You can change how many lines you want to view as well.

head -n (# of lines) (filename)

and that is it for today's article

yayme

Thank you for reading it πŸ₯³ For future Linux content, give me a follow here or follow my twitter, @linuxseb

Oldest comments (11)

Collapse
 
pentastra profile image
Pentastra

Hey Seb, great content for beginners. I just noticed typo in point 6 mdkir (it should be mkdir).

Collapse
 
michaelcurrin profile image
Michael Currin

Indeed. Heading needs fixing.

Collapse
 
yhoung24909577 profile image
obiabo Immanuel

ls is restricted on cmd it dosnt work cmd uses dir but ls works on other terminals

Collapse
 
cerlancism profile image
Chen Yu

If you have git installed you can use git bash for these commands on Windows.

Collapse
 
moopet profile image
Ben Sinclair

Hasn't cmd been replaced almost entirely with Powershell now? I know it's still there, but all the menu links and things are for Powershell since Windows 10.

If you want to be nitpicky, cd also doesn't take you into "any directory you want", but rather "any directory that you have permissions for" and not always then, depending on how it's mounted.

Thread Thread
 
yhoung24909577 profile image
obiabo Immanuel

wow thats awesome

 
yhoung24909577 profile image
obiabo Immanuel

Wow this is amazing thanks a lot sir

Thread Thread
 
abcsxyz profile image
AbcSxyZ

Availability of tools do not depends of the used shell, but depend of program installed on the computer.

Commands may depends if theyr are builtin, like cd. All other commands just require to be installed.

Collapse
 
jessekphillips profile image
Jesse Phillips

Interesting that you mention head, but not tail. Tail has great value for log files.

Collapse
 
linuxseb profile image
Seb

LOL I KNOW. I honestly couldn't tell you why

 
abcsxyz profile image
AbcSxyZ

ONLY builtin commands depends upon the shell program you are running, other commands depend on the system.
cd is a builtin (it modifies state of the shell), ls, cat and any other commands of the article are executable installed on the computer.

Some softwares have a GUI version, all of them can be used in a terminal, with a more or less advanced CLI.

I've no idea what "shell tools" mean in this context.

Just to let you know, I wrote a shell as a school project, I've some internal understanding of the topic.