The Get-ChildItem cmdlet command has been widely known within the PowerShell commands for getting some items and their child items as well. These items can be fetched from one or many locations from the system folders. So, within this article today, we will be discussing the PowerShell Get-ChildItem command in the PowerShell of Ubuntu 20.04 Linux operating system. You must start the command terminal first within the Ubuntu 20.04 system. Use “Ctrl+Alt+T” to do so. Install and configure PowerShell within the terminal and open it with the pwsh query, as shown in the snapshot below.
Let’s have a look at all the files and folders located in the main home directory of a Ubuntu 20.04 system. So, we will be using the Get-ChildItem command to get those folders or files in later examples. To list the files and folder, try the simple keyword “ls” as shown below.
Example 1
Let’s have our first example for using the Get-ChildItem cmdlet to get the items from a home directory. We will not mention the specific folder here, as we want all the child items, i.e., files and folders of a home directory. So, we have used the cmdlet “Get-ChildItem” solely on the PowerShell to get the whole child items of the home folder as shown in the picture. We have got all the text files and folders of the home directory and not the child items of its folders.
Example 2
Let’s take the Get-ChildItem command on the shell in a different way. In the above illustration, we have all the information regarding the child items of a home directory and their names. Within this example, we will be using the parameter “Name” only to get the names of child files and folders of the Home directory. So, the command results in the names of the child files and folders on the shell while using the parameter “Name”.
Example 3
Let’s make the use of the “Get-ChildItem” command a little more specific. So, we have been using this command to only get the child items from the home directory, which contains the “txt” extension at their end. We have used “*.txt” as a parametric value. In return, we have got only 1 record for a text file, and its additional information is shown below.
Let’s use the same query for child items containing the “sh” extension at their end. As a result, we have got 3 bash files and their information on the shell.
Example 4
Let’s check for the child items within another folder of the Ubuntu 20.04 system. So, we have to mention the path to that particular folder to get its contents, i.e., files and folders as children. Thus, we mentioned the path within the Gtr-ChildItem command to get into the Documents folder of our Linux system. The Path parameter has been used here to specify the location path. After the execution of this command, we have got two records as the child items of a Document folder, i.e., text files on the screen shown below.
You can also use the Name parameter after the path parameter to get the names of child items instead of their whole information as below.
Example 5
Here comes the Recurse parameter within the Get-ChildItem cmdlet. It is used to get the child items of a specific item and get all the child items of those child items as well. Therefore, we have applied the Get-ChildItem on our home directory without mentioning any path along with the Recurse parameter. In return, it displays the list of all files and folders residing in the home folder. In addition, it also displays the child items, i.e., files and folders of those folders as well which are displayed in the first place. As you can have a look, that the Documents folder child items are displayed as well.
The snap folder contains another folder, “PowerShell” as its child folder.
The sub-files and folder for the child folders, i.e., PowerShell, and Downloads have been displayed as well.
You can see all the child sub-files and folders of a “tmp” folder from the image below. The “tmp” folder is most used in system configurations, so a long list has been displayed.
Example 6
Let’s make use of another parameter in the Get-ChildItem cmdlet in the PowerShell named “Exclude”. This parameter has been known for ignoring or excluding the files or folders having their name started with some particular character or string of characters. So, we have used the Exclude parameter here in the Get-ChildItem command to exclude all the files or directories having their name started with the character “D”.
Upon executing this updated query, we have got all the files and folders in return as output, but the files and folders having names starting with alphabet “D” are ignored. You can see the Get-ChildItem cmdlet query using the Exclude parameter in the image below, along with the output.
Example 7
Let’s have a look at another way to use the Get-ChildItem command in the PowerShell of the Ubuntu 20.04 system. So, we will be making use of variables this time. Here, we have used the Get-ChildItem command to fetch all the child items of the home folder ending with “txt” extensions. The result would be saved to the variable “v”. After this, we have used the “Format-Wide” cmdlet to show the contents of this variable in a different format on our PowerShell screen. The InputObject parameter takes the variable as an argument. As a result, only a single record has been displayed on the PowerShell screen.
The same goes for the parameter Path that is used in the Get-ChildItem command as below.
Conclusion
This article explains very simple examples for using the Get-ChildItem cmdlet within the PowerShell of the Ubuntu 20.04 Linux system. We have used different parameters, i.e., exclude, Recurse, name, and path, to make it more understandable. This was all about Get-ChildItem in the PowerShell. We hope you will like it.