Easy way to Manipulating files in PowerShell

181

In this post, we will create files by hand at the command line. Creating files at the command line is a valuable skill in itself. We will begin by learning how to redirect output. This will allow us to use the ”echo” command to create a new file at the command line. We’ll also learn how to add more lines to the end of the file – a process known as “appending”. Next we’ll learn various ways to list the details of the files, including both, short and long forms. Finally, we’ll learn how to rename, copy, and delete files.

Redirecting and Appending

Here we shall learn how to create a new file with the command line.
To add some text to a file which needs to be created, we do it like this:


echo "This is some text" > somefile.txt

The code above will create a file named ‘somefile.txt’. It will add the text too. If we want to leave the file empty, just leave the “” empty.

To append text to that file, do it like this:

echo "This is more text" >> somefile.txt

The above code will add another line of text to the file.
Finally, to check what’s in the file, and if it exists, run ‘concatenate‘ command:

cat somefile.txt

Listing files and folders

To list all files and folders use the command ls
To list only pdf files run this: ls *.pdf
You can use this example for any file type.
Use man ls command to see more options for using the ls command.

Navigating through folders

Go one level up: cd ..
to enter in folder listed use: cd foldername
(change ‘foldername‘ to actual folder name you want to enter)

Renaming, copying, deleting files

To rename a file called “file1.txt” to “myfile1.txt”, use this command: mv file1.txt myfile1.txt
To make a copy of a file use: cp myfile1.txt thecopyofit.txt
To permanently delete a file use this: rm thecopyofit.txt

Wrap it up

These commands work in any OS, including Windows, Linux, Mac. Use them as you wish.
Thanks for reading.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

CAPTCHA