0% found this document useful (0 votes)
21 views3 pages

Finding Information - Transcript

This document provides an introduction to several Linux commands for manipulating directories, searching for files, and viewing command history. It demonstrates how to create and delete directories with mkdir and rmdir, search for files with find, view command history with history, and get help for commands with man. Key commands covered include mkdir, rmdir, touch, ls, find, grep, history, and man.

Uploaded by

bhuttoahmed2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

Finding Information - Transcript

This document provides an introduction to several Linux commands for manipulating directories, searching for files, and viewing command history. It demonstrates how to create and delete directories with mkdir and rmdir, search for files with find, view command history with history, and get help for commands with man. Key commands covered include mkdir, rmdir, touch, ls, find, grep, history, and man.

Uploaded by

bhuttoahmed2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Welcome back!

In the last task, you got familiar with text


editors and commands that let you manipulate files.
In this task, we'll learn how to create and delete directories,
how to search for files or content, and a few ways to see
the commands you've used.
First, let's start by creating a new subdirectory in our user
directory.
Let's call it 'newDir', and we'll use the command mkdir
to create it:
'mkdir newDir'. 'ls -al'. As you can see, mkdir created a new directory
with the name we supplied. But eh, let's call it 'backups'
instead.
How? You'll recognize this command: 'mv newDir backups'.
Now, 'ls -l'...
And there's our directory called backups.
Let's put some files in there with touch.
We can cd into that directory and use touch,
but let's stay right here and use a relative path to make
it happen.
'touch backups/file1'
'touch backups/file2'
Let's look at what's in the backups directory.
'ls -l backups'
Touch successfully created new files.
Now what if we don't want this directory here anymore?
Let's try the rmdir command and see what happens.
'rmdir backups'
Oops, we got an error.
Why? rmdir only works on empty directories, which is kind
of nice
on those days you get a little overzealous and accidentally
nuke a directory full of files you need. Instead, we'll use rm
again, this time to recursively
move through that directory, delete its contents, and then
delete the directory.
Please note this can be a dangerous command.
Make sure you're deleting the right thing before you start.
For an added layer of safety, I'm going to use an extra
option
so that rm prompts me before deleting that file--
that's 'i'--and then 'r' to tell it to go recursively through
the backups directory:
'rm -ir backups'
It's asking me if I want to delete the contents, one by one.
Yes, to all of these, since we do want to delete everything
in the backups directory. Now let's look at the user
directory contents again: 'ls -al'
It's gone. How do we find a file
we need? Say we know its name, but we have no idea where it is.
Let's use
find. First, we'll create a couple of directories and then touch
a file:
'mkdir newdir'
'mkdir newdir/backups'
'touch newdir/backups/cleanbackup'
'touch newdir/backups/cleanbackup'
Now let's go to the root directory, the top-most level, and ask
find to locate a file.
Pretend we don't remember what the actual file name is, but
we remember it had 'backup' in it.
'cd /'
Let's tell bash to find a file somewhere in the
directory structure starting at the root directory
(so that means we're asking it to look at every file), and the
base name of the file is backup, so:
'find / -name "backup"'
And then we see a bunch of errors
racing past too fast to see normally.
If it's really bad, you can type control-c to escape
out of that. That's the command to kill the current process,
whatever's happening.
So what happened here?
You can look at the output and see we got permissions notices.
Those are standard error messages when the command can't
access something based on the permissions you have.
So let's tell the shell to try again only without all of
the error notifications:
'find / -name "backup" 2>/dev/null'
What did I add there?
2>/dev/null is syntax
where the two tells the shell to take error output and redirect it
to /dev/null where it's deleted.
There are no spaces, by the way. Now that we've run this and gotten
output we can read, we don't see our file.
That's because bash was looking for a file with only the word
'backup' in the base. So it would return 'backup'
but not 'mybackups' or 'weeklybackups'.
Let's use asterisks, which are shorthand for 'there might be
other characters here' and tell the shell to look for the word
backup, possibly within other text.
'find / -name "*backup*" 2>/dev/null'
'find / -name "*backup*" 2>/dev/null'
Now we can see our file and the path to it.
A command I use a lot is grep.
Grep tells the shell to search for a regular expression.
You don't need to know what a regular expression is to use
grep, though I recommend learning about them.
We can use grep to search through the contents of a file like this:
'grep python /usr/bin/pydoc'
'grep python /usr/bin/pydoc'
What we've done here is tell grep we're searching for all instances
of 'python' in the pydoc file at usr/bin, but you can also
use grep on the command line. To limit
what another command outputs, we'll use a pipe.
It's the absolute value vertical line, and it tells the shell
to take the output from the first command and feed it to the
command on the other side of the pipe.
Let's try this with the find search we used before. Let's say
we only want to find files with 'backup' in the name that
appears somewhere in our user directory.
We know our user name will appear there.
So let's grep for that--in other wordsc strip out any output
that doesn't have the words 'backup' and 'rhyme' somewhere in that
line. But let's also use the global variable that refers to
our user name.
Brian slash rash name, quote, asterisk, Crack up estrus,
'find / -name “*backup*” 2>/dev/null | grep $USER'
'find / -name “*backup*” 2>/dev/null | grep $USER'
The shell took that list of files and directories from the find
search and piped it to grep, which looked for 'rhyme',
the username the $USER variable refers to. What we're
seeing here is the combined output of the two commands.
Now let's learn a few shortcuts when you find yourself using
the same commands over and over. First is the up-arrow key.
Press it and see what happens.
You can also press the down key to go back.
This is the list of commands you viewed since you opened this
terminal.
This is great for quickly accessing a series of commands you
need to execute multiple times.
Now, let's look at the exclamation or the bang.
'!ls'
What's this?
Well, it's the entire command the last time we used ls.
What if we want to execute the last command and our
up-arrow is broken?
'!!'
There's the ls again because it's the last command we used.
Did you know bash is
keeping a record of your command
history?
You can access it with the history command history:
'history'
There's the history of the commands we've used,
even after we logged out and shut the computer off. They're
all recorded in the history file
so you have access.
Bash doesn't keep an unlimited number, though, so don't count
on being able to access your very first command on the system
if it's been a while. And consider, you can pipe this output to
grep. How would you look at all the commands where you used
cat?
'history | grep cat'
There they are, including this command.
We've talked about options some, but how do you know what the
options are?
Let's revisit man, short for manual, like user manual.
Let's look at find's manual:
'man find'
As you can see, there are a lot of options to customize your
filename
search. Use man
any time you're not familiar with the command or when you
want to know if you can change how it executes. That's it for
this task where you've learned directory manipulation,
command history, and how to get details on a command. In the
next task, we're going to use what we've learned so far to
create aliases.

You might also like