G1 - command line

basic navigation

command function example windows equivalent
pwd print working directory $ pwd/Users/sujaya cd
ls list files in the directory $ ls dir
cd <folder> change directory $ cd Documents
cd .. go up one level $ cd ..
cd ~ go to home directory $ cd ~

working with files

command function example windows equivalent
touch <filename> create an empty file $ touch file.txt type nul > <file>
or
echo.> <file>
mkdir <folder> create a folder $ mkdir Projects
rm <file> delete a file $ rm file.txt del <file>
rm -r <folder> delete a folder $ rm -r Projects rmdir /s <folder>
cp <source> <destination> copy file/folder $ cp file.txt backup.txt
mv <source> <destination> move/rename $ mv file.txt oldfile.txt
echo "<text>" > <file> add text to file $ echo "hello world" > file.txt
echo "<text>" >> <file> append text to file $ echo "hello world" >> file.txt

viewing file contents

command function example
cat <file> show file content $ cat file.txt
less <file> view long files page by page $ less file.txt
head <file> show first 10 lines $ head file.txt
tail <file> show last 10 lines $ tail file.txt

searching

command function example
grep "<text>" <file> search for text in a file $ grep "hello" file.txt
find . -name "<filename>" search for a file in current folder $ find . -name "*.txt"

cheat sheet

task linux / mac os windows command prompt windows powershell notes / tips
show current directory pwd cd get-location powershell’s get-location shows full path
list files/folders ls -l dir get-childitem or ls powershell aliases ls to get-childitem
change directory cd folder cd folder set-location folder or cd folder cd .. goes up one level
create folder mkdir folder mkdir folder new-item -itemtype directory folder works in both cmd and powershell
create empty file touch file.txt type nul > file.txt new-item file.txt echo > file.txt also works
copy file cp file1 file2 copy file1 file2 copy-item file1 file2 use -recurse for folders in powershell
move / rename file mv file1 file2 move file1 file2 move-item file1 file2 can rename files with same command
delete file rm file del file remove-item file -recurse for folders in powershell
delete folder rm -r folder rmdir /s folder remove-item folder -recurse dangerous: deletes all contents
view file content cat file type file get-content file get-content can also stream large files
view first/last lines head file / tail file n/a get-content file -totalcount 10 / `get-content file select-object -last 10`
search inside files grep "text" file findstr "text" file select-string "text" file works recursively with -r in linux
find files/folders find . -name "*.txt" dir /s *.txt get-childitem -recurse -filter *.txt searches recursively in subfolders
environment variables echo $var echo %var% $env:var useful for paths, configs
run program ./program program.exe .\program.exe linux executables may need chmod +x
clear screen clear cls clear-host keeps terminal tidy
history of commands history doskey /history get-history powershell allows re-running with invoke-history
redirect output to file command > file command > file command > file.txt appends with >>
pipe output `command1 command2` `command1 command2`
check system info uname -a systeminfo get-computerinfo linux shows kernel, architecture, etc.