Working with Files
Commands
The following commands are commonly used commands for working with files.
cat # show the contents of a file
more # show the contents of a file, forward scrolling/jumping
less # show the contents of a file, forward and backward scrolling
cp # copy a file
mv # move a file, rename a file
wget # download stuff (sudo apt install wget)
xdg-open # open a file or url in its default applicationExample
Navigate to Desktop and create a books and music directory
cd ~/Desktop mkdir books musicNavigate into the newly created books directory and download a book
cd books wget https://www.gutenberg.org/cache/epub/98/pg98.txt lsRename the book
mv pg98.txt a-tale-of-two-cities.txt more a-tale-of-two-cities.txtDownload another book
wget https://jcoriell.github.io/csc222/files/cnbc.txt more cnbc.txtNavigate to the music directory from the books directory and download a song
cd .. cd music wget https://jcoriell.github.io/csc222/files/mbe.mp3 xdg-open mbe.mp3 # open it with the default application for the system
Text Editors
vim (Vi - Improved)
- steep learning curve, but frequent users love it
- will make you better that everyone else
- can learn using the command
vimtutor
Open with the command: vim [filename]
Vim Modes:
insert - i to activate, escape to back out
replace - r to enter replace mode, use it to write over text instead of inserting text
- escape to back out
visual - v, V, or ctrl+v to activate character, line, or block visual mode
- used to select text and apply commands to that text
- ex: copy, cut, replace, indent, search, and more...
- escape to back out
command - : to activate. Some common commands:
- w to save
- q to quit
- wq or x to save and quit
- q! to quit without saving
- e [filename] to open a file
- saveas [filename] to save the file as a new name
- [an integer] to jump to a line
Example
Use the following two commands to create a file somewhere on your system.
cd ~/Desktop/programs # navigate to a folder to complete the example in vim random_number_generator.py # open a file (it is ok if the file doesn't exist yet)At this point vim should have opened.
Type
ito enter insert mode and type the following code.import random print(random.randint())Now hit
escapeto exit insert mode.Move the cursor to the beginning of the word randint
Hit
vto enter visual mode.Use the
ekey to go to the END of the word (bgoes to the Beginning)Use the
ckey to CHANGE the selected text torandom. (we are replacing randint withrandom)At this point the python code should look like this:
import random print(random.random())Use
escapeto exit insert mode.Use
:to enter command mode.Type
wqand hit enter to save and exit.If you’d like to run the code, use the following command:
python3 random_number_generator.py
It is recommended at this point that you run the vimtutor command if you’d like to learn more about using vim.
Nano
- a simple to use terminal based text editor
- little to no learning curve
Example
In this example, we create a file and add “hello world” to it using nano.
Run the following commands
cd ~/Desktop/programs # navigate to a folder to store a file touch helloworld.py nano helloworld.pyAt this point, the file should be open. Type
print("hello world")on the first line.Type
ctrl + xto be prompted to save and close.Run
cat helloworld.pyto see the contents.Run
python3 helloworld.pyto run the file.