In Bash, which of the following commands can you use to view the contents of a document
Working with FilesOur data set: World Development IndicatorsNow that we know how to navigate around our
directory structure, let’s start working with our data files. The World Development Indicators dataset is stored in the WildcardsNavigate to your
We are interested in looking at the CSV files in this directory. We can list all files with the .csv extension using the command:
The This command: lists only the file that ends with This command: FIXME: is this working on git bash? Lists every file in
Command HistoryIf you want to repeat a command that you’ve run recently, you can access previous commands using the up arrow on your keyboard to go back to the most recent command. Likewise, the down arrow takes you forward in the command history. A few more useful shortcuts:
You can also review your recent commands with the to see a numbered list of recent commands. You can reuse one of these commands directly by referring to the number of that command. For example, if your history looked like this:
then you could repeat command #260 by entering: Type
Examining FilesWe now know how to switch directories, run programs, and look at the contents of directories, but how do we look at the contents of files? One way to examine a file is to print out all of the contents using the program Enter the following command from within the This will print out all of the contents of the
Enter the following command: Some navigation commands in
Shortcut: If you hit “/” then “enter”, For instance, let’s search forward for the word
Remember, the There’s another way that we can look at files, and in this case, just look at part of them. This can be particularly useful if we just want to see the beginning or end of the file, or see how it’s formatted. The commands are
The
This is particularly useful for CSV files, as they often have a column header in their first row.
Creating, moving, copying, and removingNow we can move around in the file structure, look at files, and search files. But what if we want to copy files or move them around or get rid of them? Most of the time, you can do these sorts of file manipulations without the command line, but there will be some cases (like when you’re working with a remote computer like we are for this lesson) where it will be impossible. You’ll also find that you may be working with hundreds of files and want to do similar manipulations to all of those files. In cases like this, it’s much faster to do these operations at the command line. Copying FilesWhen working with computational data, it’s important to keep a safe copy of that data that can’t be accidentally overwritten or deleted. For this lesson, our raw data is our CSV files. We don’t want to accidentally change the original files, so we’ll make a copy of them and change the file permissions so that we can read from, but not write to, the files. First, let’s make a copy of one of our CSV files using the Navigate to the
We now have two copies of
the Creating DirectoriesThe Moving / RenamingWe can now move our backup file to this directory. We can move files around using the command
The
File PermissionsWe’ve now made a backup copy of our file, but just because we have two copies, it doesn’t make us safe. We can still accidentally delete or overwrite both copies. To make sure we can’t accidentally mess up this backup file, we’re going to change the permissions on the file so that we’re only allowed to read (i.e. view) the file, not write to it (i.e. make new changes). View the current permissions on a file using the
The first part of the output for the Here the three positions that relate to the file owner are Our goal for now is to change permissions on this file so that you no longer have
RemovingTo prove to ourselves that you no longer have the ability to modify this file, try deleting it with the You’ll be asked if you want to override your file permissions:
If you enter Important: The By default, Enter the following command: This will delete not only the directory, but all files within the directory. If you have write-protected files in the directory, you will be asked whether you want to override your permission settings.
Which command is used to see the context of a file?Commands for displaying file contents (pg, more, page, and cat commands) The pg, more, and page commands allow you to view the contents of a file and control the speed at which your files are displayed. You can also use the cat command to display the contents of one or more files on your screen.
Which command shows the contents of the current working directory?Use the ls command to display the contents of a directory. The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags.
Which of the following commands can be used to search text files?You need to use the grep command. The grep command or egrep command searches the given input FILEs for lines containing a match or a text string.
What is the command to list the contents of a directory in Unix and Unix like operating systems?The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.
|