Powershellcmds
Powershellcmds
way to manipulate a machine. Even though GUI applications remain popular among vast
users, a command-line interface gives users more control over their computer by
running text commands. It’s a powerful way to interact with a computer. For
example, you can run text commands to create 100 folders, but it will take more
time and effort to achieve the same result via a GUI application.
mkdir test
cd test
for i in {1..100}
do
mkdir "$i"
done
To be able to run these commands, we need to use the Terminal application. This
Terminal provides you with the shell. Shell is an environment where we can run our
shell commands and shell scripts. It allows us to control command-line tools. The
macOS X comes with the bash shell (Bourne Again shell), but you can change your
shell to csh (C shell), zsh (Z shell), ksh (Korn Shell), tcsh (TENEX C Shell), etc.
Now let’s go through some basic shell commands to see what is happening. List all
available shells in macOS X.
cat /etc/shells
To see which shell you’re running.
echo $0
To change to bash shell.
chsh -s /bin/bash
To change to zsh shell.
chsh -s /bin/zsh
Depending on which shell you’re using, you’ll get different features. My choice of
shell is zsh. You’ll learn some basic shell commands from this post to improve
daily workflow by saving time and effort.
1. pwd
The pwd command stands for Print Working Directory. It will print the full path to
the current working directory.
pwd
# /Users/universe/Desktop
The shell ignores a word if it starts with # (hash) and any remaining characters on
that line.
# /Users/universe/Desktop
2. mkdir
The mkdir command stands for Make Directory. Here we are creating a directory on my
Desktop called neptune.
mkdir neptune
We can create multiple directories using the -p option.
mkdir -p space/neptune
The above command will create a folder called space and a nested folder or
subfolder neptune. If we want to create our third folder naiad under neptune, we
can use the following command. We don’t have to provide the -p option for this
command because the path space/neptune exists already.
mkdir space/neptune/naiad
3. cd
The cd command stands for Change Directory. Let’s use this command to access the
space directory we have created previously.
cd space
cd neptune
cd naiad
Or you can use the following command to access the naiad directory.
cd space/neptune/naiad
Both commands will produce the same result. Now we should be inside the naiad
directory.
pwd
# /Users/universe/Desktop/space/neptune/naiad
cd ..
pwd
# /Users/universe/Desktop/space/neptune
Moreover, we can move to multiple levels. In this case, let’s move 2 levels up.
cd ../..
pwd
# /Users/universe/Desktop
Now, we are back to the Desktop directory.
The . (single periods) represents or points to the current working directory. It’s
helpful when we don’t want to write the full path when running a command. For
example, if our command-line tool is in the current directory, we can use the
following command.
./space-robot
The ~ (tilda) or $HOME represents or points to the home directory. For macOS X, the
user’s home directory is under the /Users directory.
cd ~
pwd
# /Users/universe
cd $HOME
pwd
# /Users/universe
By using this shorthand, you don’t have to type the full path to the home
directory.
cd /
4. touch
Using the touch command, we can create a file.
cd ~/Desktop/neptune
touch todo.txt
5. ls
The ls command stands for List. We can use it to list all the contents of the
specified directory; if no path is specified, it will list everything in the
current directory.
cd ~/Desktop/neptune
mkdir todo
ls
# todo todo.txt
Use -a flag to list hidden files and directories.
ls -a
Use -l flag to list with detailed information.
ls -l
Or use it together.
ls -al
6. clear
We have been writing commands for a while, so let’s clear the Terminal screen.
clear
7. mv
The mv command stands for Move. We can use this command to move files and
directories from one place to another. Also, we can use it to rename files and
directories. The following command will move the todo.txt file from its current
directory to its child directory todo.
cd ~/Desktop/neptune
mv todo.txt todo/todo.txt
Now let’s rename the file. Here we are renaming the todo.txt file to my-todo.txt.
cd todo
mv todo.txt my-todo.txt
8. cp
The cp command stands for Copy. Now, let’s copy our my-todo.txt file to the parent
directory.
cd ~/Desktop/neptune
cp todo/my-todo.txt my-todo-bu.txt
We can use the -r flag to copy a directory. The following command will copy
everything that is inside the todo folder to a folder called bu.
cp -r todo bu
9. rm and rmdir
The rm command stands for Remove, and rmdir command stands for Remove Directory.
The rmdir command can only delete a directory if it is empty. Now let’s remove a
file and directory.
cd ~/Desktop/neptune
rm my-todo-bu.txt
mkdir empty
rmdir empty
We can use the -r flag to delete a non-empty folder.
rm -r bu
rm -r todo
10. > < * ? [] ; $ \
The > can be used to redirect stdout (Standard output). Here, we are adding a task
to our todo.txt file. The echo command writes its arguments to stdout. This >
command will overwrite the todo.txt if it exists already.
cd ~/Desktop/neptune
echo 1. Make space robot > todo_today.txt
If we want to append data, we need to use the >> command.
wc < todo_today.txt
# 2 9 43
The * (asterisk) can be used as a wildcard to match zero or more characters.
ls *.txt
# todo_today.txt
The ? (question mark) can be used to match a single character.
ls ????_?????.txt
# todo_today.txt
The [] (square brackets) can be used to match any number of characters inside them.
ls t[o]do_*.???
# todo_today.txt
The ; (semicolon) can be used to write multiple commands on a single line; all we
have to do is separate each command with a semicolon.
echo $HOME
# /Users/universe
MyValue=99
echo $MyValue
# 99
The \ (backslash) can be used to escape a special character.
echo \$MyValue
# $MyValue
echo \$$MyValue
# $99
11. cat
The cat command stands for Concatenate. It prints the contents of a file or files
to stdout. Print content of a single file.
cd ~/Desktop/neptune
cat todo.txt
# 1. Make space robot
# 2. Make space robot v2
Print content of multiple files.
cd ~/Desktop/neptune
echo FILE 1 > file_1.txt
echo FILE 2 > file_2.txt
echo FILE 3 > file_3.txt
cat file_1.txt file_2.txt file_3.txt
# FILE 1
# FILE 2
# FILE 3
12. grep
The grep command stands for Global Regular Expression Print. It searches text
inside the file specified with the given search pattern. By default, grep is case-
sensitive. For case-incentive search, we can use the -i flag.
cd ~/Desktop/neptune
grep "FILE" file_1.txt
# FILE 1
grep -i "file" file_1.txt
FILE 1
The -r flag is used to search inside a directory recursively. Here it will search
everything inside the current directory and its child directories.
grep -r "FILE" .
# ./file_1.txt:FILE 1
# ./file_2.txt:FILE 2
# ./file_3.txt:FILE 3
There are other flags that can be useful, like the -n flag will print matching
lines with the line number, the -c flag will print the count of the matching lines.
13. |
The | command or the pipe command is used to add two or more commands, where the
output of the previous command is used as the input for the next command.
cd ~/Desktop/neptune
head -n 1 todo.txt
# 1. Make space robot
Print the last 1 line of a file.
tail -n 1 todo.txt
# 2. Make space robot v2
These commands can be useful when working with large files.
15. find
The find command is used to search files.
cd ~/Desktop/neptune
find . -name "*.txt"
# ./file_1.txt
# ./todo.txt
# ./file_2.txt
# ./file_3.txt
16. open
The open command can be used to open a file and open the directory using the Finder
application.
cd ~/Desktop/neptune
open .
open todo.txt
17. man
Finally, we have the man command, which stands for Manual. It prints the user
manual for a command.
man cd
Use the up and down arrow keys to navigate through the documentation. Use the f key
to move forward one page and the b key to go back one page — press q to quit.
Congratulations! Now you know all these cool shell commands, which will help you to
save time and effort. You can find all the shell commands I've discussed on my
GitHub. Happy coding!