Lecture notes 2#

Command line basics#

In the examples below we use $ for a bash prompt (Mac/Linux) and > for a Windows CMD prompt. You do not type these yourself, they are printed to the screen by the computer as signal that it is waitin for input

Go to https://swcarpentry.github.io/shell-novice/

This represents workshop material for learning about the bash shell.

Here we limit ourselves on sections 1-3, on looking at the file system from the comman line

Commands used in section 1

pwd - print working directory
cd  - change direcory 
ls  - list files

Getting help/documentation#

Many programs in bash have documentation saved in man pages. Others have it built-in with an option like -h or --help

Mac/Linux#

$ man ls
$ ls --help

Windows#

> dir /?

pwd#

In the command line we have the concept of active folder, or “where am I?”. pwd displays the current folder on the screen. It is common these days that computers have by default this folder as a part of the terminal prompt. For bash it can be redefined to your liking by modifying the PS1 shell environment variable

$ PS1="What do you want? "
What du you want? █

cd#

To move around in the file system we use the cd command for change directory. When you provide an argument it can either be the full path of a folder or relative path. The full path starts with the root / on mac/linux and a drive letter in windows C: in windows

To take me to the home folder for user jane something like this would be appropriate

$ cd /home/users/jane
> cd C:\Users\Jane

In mac/linux the forward slash / is used to separate parts of a file path and in windows the it is the backslash \

Relative paths are valid file names and can begin with .. for parent folder or simply the name of a subfolder from the current folder

A special character in bash is the ~ which is short for the home folder

cd without argument works differently in mac/linux vs windows

bash/linux

$ cd   # takes me to my  home folder, equivalent 

windows

> cd   # displays the current folder without changing anything

ls/dir#

List the files in current or other folder

examples

list files in current folder

mac/linux

$ ls  

windows

> dir /b  # restrict output to filenames

list hidden files

$ ls -a
> dir /a

mkdir#

create new direcory (folder). Usage

mac/linux#

$ mkdir newfolder
$ mkdir -p a/b/c

to create several subfolders at once the -p option is needed

windows#

> mkdir newfolder
> mkdir a\b\c

rmdir#

To delete an empty folder

mac/linux#

$ rmdir emptyfolder

windows#

> rmdir emptyfolder

To delete an non-empty folder (a direcory tree)

mac/linux#

$ rm -r folder_with_content

windows#

> rmdir /s folder_with_content

Copying files#

Create a duplicate of a with a new name

Mac/Linux#

$ cp  oldfile newcopy 

Windows#

> copy oldfile newcopy

Copy an existing file to a new location - an existing folder

Mac/Linux#

$ cp oldfile otherlocation/

WIndows#

> copy oldfile otherlocation\

Copy a whole directory (backup)

Mac/Linux#

$ cp -r oldfolder backupfolder

Windows#

> xcopy oldfolder backupfolder\

Table#

Table with equivalent linux/windows commands

mac/linux

windows

explanation

pwd

cd (without arguments)

print work directory (current active folder)

cd folder

cd folder

make folder the current work directory

cd (without arguments)

cd %USERPROFILE%

make the home folder the current work directory

ls

dir /b

list files in current folder, names only

ls -l

dir

list files in current folder, in long format

mv

rename, move

rename and/or move

cp

copy

copy files

cp -r

xcopy

recursive copy of folder and its subfolders

rm

del

delete file

mkdir

mkdir

create folder

rmdir

rmdir

delete folder

man command

command /?

look up documentation for command …