top of page

Linux- Manage files

Rebecca Boardman

At the start of the year I accepted a role to become a DevOps engineer. I’ll be transitioning over properly in the middle of the year but till then I’ve been using the time to upskill myself in areas I think will be beneficial. One of those skills is Linux. The popularity of Linux only seems to be growing and growing these days especially when it comes to utilising this with cloud service providers like AWS. Coming from a SQL DBA background I’ve only ever heavily used Microsoft products- therefore when it comes to Linux its fair to say I am a complete rookie! I thought it might be beneficial to start writing these things as I learn them- because the chances are there will be similar people to myself who want to learn and just don’t know how to get started. I really hope this blog can give an insight and teach people something perhaps they didn’t know before. Also give the confidence that pushing outside of your comfort zone and learning something new is actually pretty cool!


Ive started by downloading and installing WSL 2 on my computer and also downloaded the Ubuntu App from the windows store to get me started. You may wish to do the same to follow along with this blog. First lets start out with something everyone needs to do which is managing files so open your Ubuntu terminal.


Useful Commands for managing files


pwd : this command will tell you exactly where you are in the file system


mkdir : Creates a directory just specify this command and the name you wish to call your directory. Note you can make more than 1 directory at a time by specifying spaces in the name for example I want to make 3 directories. "Test1", "Test2" and "Test3". You could just simply issue the below command:

mkdir Test1 Test2 Test3

ls : List the directory contents

CD : Change directory - this will change into the directory you wish. For example we want to change into the directory of Test1 that we've just created. Simply type the following command

cd Test1

It's also handy to note you can use the following command to go one level up in the folder structure

cd ..

touch : The touch command can create files but also if the file already exists can change the file timestamps. For example lets create 2 files within our Test1 folder. If your not already there issue the CD Test1 command as mentioned earlier.

touch File1 File2

This will then create 2 files. Run the below command to list current files and add a -l. This will show you more information such as file owner and the last time the file was modified.

ls -l

You'll see both files in there which should at this moment of time have the same date, but what if we wish to amend the time for File2. Well just run the following:

touch File2

run the following again to list the last modified date and you'll notice File2'S date has changed

ls l 



cp : Copy file. For example we want to copy file1 which we have created within the test1 folder to the test2 folder

cp ~/Test1/File1 ~/Test2/CopyofFile1
cd ..
cd Test2
ls -l

You'll see a file now exists "CopyofFile1"


mv : This command can move a file or rename the file for example say instead of CopyOfFile1 we want to call this File1 issue the following command:

mv CopyofFile1 File1

now if you issue the ls command you'll notice the file name has now changed to File1.



rm : Removes a file

rm File1

If you wish to remove a directory and all the files within it you can issue the following eg I wish to delete the Test1 Folder

rm -r Test1

rmdir : This will delete the given directory eg we wish to remove the Test3 directory

rmdir Test3

vi : This can be used to create files when issuing the command and the file doesn't exist it will open up a blank text editor

vi testfilecreation.txt

If this has created a new file you'll notice it'll pop up a blank editor that looks like this



click on insert on your keyboard this will allow you to start typing.


Type anything you want eg "this is just a test"


Once your done press escape on your keyboard

Then type the following

:wq

This will save the file. There are also other options you can run:

:wq Write, then quit

:q Quit (will only work if file has not been changed)

:q! Quit without saving changes to file



cat : Will print the file on the screen for example the file we've just created run the following

 cat MyRandomtextfile.txt


Useful to know Cat can also be used to write text to a new file. For example run the following command to create a file "tester"

cat > tester

hit enter and type in the following

lets create this as the first line

Now hit enter and press CTL D once your done.


We can also add in new lines to an existing file. By running the following

cat > tester

hit enter and type in the following

lets now add my second line here

Now hit enter and press CTL D once your done.

Run the cat command once again to display your file and you can see the below


more : File viewer which allows you to page through the file


less : File Viewer that allows you to move down one line in a time


nano : This is another text editor you can use with some really cool functions. To create a file type in the following

nano test

an editor will pop up that will look like the below:



Paste in any kind of text you want. I'm going to just copy in a SQL script using Ctrl C to copy and Ctrl V to paste into the nano screen.


You can use your up and down arrows to scroll through the document. Say we wish to replace something on the document. Do the following

ctl \

Type in the word you wish for replace. In my case for this example i'll type the following

hit enter. Now type what you wish to replace it with for example

hit enter again. It will then give you some options if you want to replace this instance, replace all or cancel.

Press A


You'll then notice it has successfully replaced all the instances mentioned



You can also search for words using the following:

CTRL w 

and hit enter.


To see a full list of things you can do within nano use the Get Help function by typing

CTRL g

and you'll see a list of all types of options come up. (use q to quit to return back to your file)


To save your file run the following command and hit enter

CTRL o

To quit out run the following

CTRL x

And that's all for this weeks blog post. I think we've learnt some pretty cool fundamental things when it comes to linux and managing files. You know what they say- practice makes perfect so try this out a few times. The more and more you use it the easier it will be to remember and will just be ingrained in your brain! I'll be doing more of these blog posts particularly about linux so watch this space for more!

Comments


bottom of page